[RESOLVED] Error after coding

I just finished making the second state, but when i start it, I get this error:

NullReferenceException: Object reference not set to an instance of an object
TextController.state_cell () (at Assets/TextController.cs:25)
TextController.Update () (at Assets/TextController.cs:18)

Here is all my code so far:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class TextController : MonoBehaviour {
	public Text text;
	private enum States {cell, mirror, sheets_0, lock_0, cell_mirror, sheets_1, lock_1, freedom};
	private States myState;
	// Use this for initialization
	void Start () {
		myState = States.cell;
	}
	
	// Update is called once per frame
	void Update() {
		print (myState);
		if (myState == States.cell) {
			state_cell();
		} else if (myState == States.sheets_0) { 
			state_sheets_0();
		}
	}

	void state_cell () {
		text.text = "You are in a prison cell, and want to escape. " +
					"There are some dirty sheets on the bed, a mirror on " + 
					"the wall, and the " + 
					"door, which is locked from the outside.\n\n" +
					"Press S to view Sheets, press M to view Mirror, " +
					"plus L to view Lock.";
		
		if (Input.GetKeyDown(KeyCode.S)) {
			myState = States.sheets_0;
		}
	}
	void state_sheets_0() {
		text.text = "You can't believe you sleep in these things. Surely it's " +
					"Time somebody changed them. The pleasured of prison " +
					"Life, I guess!\n\n" +
					"Press R to return to roaming your cell.";
		
		if (Input.GetKeyDown(KeyCode.R)) {
			myState = States.cell;
		}
	}
}

Have you dropped your text object into the Text property in the Inspector? You reference it in the state_cell() method.

1 Like

I coulda sworn that I already did that but I guess not. Thanks for the help!

1 Like

Hi @aseskyrocket, no worries. Glad it resolved the issue for you. Can you update you initial post and mark it resolved if you have a moment.

Thanks :slight_smile:

Privacy & Terms