I'm fairly new to C# and haven't been able to work this one out. I'm trying to make a really simple text-based game where you select a text option and it presents the next prompt. I've got the bare bones of the script seemingly working, but when I click on the buttons I get the console error "Object reference not set to an instance of an object". Could anyone help me understand what this means? And how to fix it?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
class StoryBlock {
public string story;
public string Option1Text;
public string Option2Text;
public string Option3Text;
public StoryBlock Option1Block;
public StoryBlock Option2Block;
public StoryBlock Option3Block;
public StoryBlock(string story, string Option1Text = "", string Option2Text = "", string Option3Text = "",
StoryBlock Option1Block = null, StoryBlock Option2Block = null, StoryBlock Option3Block = null)
{
this.story = story;
this.Option1Text = Option1Text;
this.Option2Text = Option2Text;
this.Option3Text = Option3Text;
this.Option1Block = Option1Block;
this.Option2Block = Option2Block;
this.Option3Block = Option3Block;
}
}
public class GameManager : MonoBehaviour
{
public TMP_Text StoryText;
public Button Option1;
public Button Option2;
public Button Option3;
StoryBlock currentBlock;
static StoryBlock block1 = new StoryBlock("Hey are you there?", "Yeah I'm here", "No run", "Who's asking?", block2, block3, block4);
static StoryBlock block2 = new StoryBlock("I never thought I'd see you again", "I wish you were dead", "Nice to see you too", "Who are you?");
static StoryBlock block3 = new StoryBlock("I knew you'd say that", "You're a joke", "Again, who are you?", "Hey mom.");
static StoryBlock block4 = new StoryBlock("I wish I'd never met you", "Cool", "Hell yeah", "Love you <3");
void Start()
{
DisplayBlock(block1);
}
void DisplayBlock(StoryBlock block) {
StoryText.text = block.story;
Option1.GetComponentInChildren<TMP_Text>().text = block.Option1Text;
Option2.GetComponentInChildren<TMP_Text>().text = block.Option2Text;
Option3.GetComponentInChildren<TMP_Text>().text = block.Option3Text;
currentBlock = block;
}
public void Button1Clicked(){
DisplayBlock(currentBlock.Option1Block);
}
public void Button2Clicked(){
DisplayBlock(currentBlock.Option2Block);
}
public void Button3Clicked(){
DisplayBlock(currentBlock.Option3Block);
}
}
I've done some poking around to try to find a solution or better explaination for what this means but am having trouble.