Having an error after coding

I just finished all making all the states and was going to test the game but I keep getting error messages saying
The referenced script on this Behaviour is missing

this is all my code:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class TextController : MonoBehaviour {
	public Text text;
	private enum States {cell, sheets_0, sheets_1, lock_0, lock_1, mirror, cell_mirror, 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();}
		else if (myState == States.sheets_1) 		{state_sheets_1();}
		else if (myState == States.lock_0) 			{state_lock_0();}
		else if (myState == States.lock_1) 			{state_lock_1();}
		else if (myState == States.mirror) 			{state_mirror();}
		else if (myState == States.cell_mirror) 	{state_cell_mirror();}
		else if (myState == States.freedom) 		{state_freedom();}
	}
	void state_cell() {
		text.text = "You are in a prison cell, and you want to escape. There are " +
					"some dirty sheets on the bed, a mirror on the wall, and the door " +
					"is locked from the outside.\n\n" +
					"Press S to view Sheets, M to view Mirror and L to view Lock";
		if (Input.GetKeyDown(KeyCode.S)) {myState = States.sheets_0;}
		else if (Input.GetKeyDown(KeyCode.M)) {myState = States.mirror;}
		else if (Input.GetKeyDown(KeyCode.L)) {myState = States.lock_0;}
	}
	void state_mirror() {
		text.text = "The dirty old mirror on the wall seems loose.\n\n" +
					"Press T to Take the mirror, or R to Return to cell";
		if (Input.GetKeyDown(KeyCode.T)) {myState = States.cell_mirror;}
		else if (Input.GetKeyDown(KeyCode.R)) {myState = States.cell;}
	}
	void state_sheets_0() {
		text.text = "You can't believe you sleep in these things. Surely it's " +
					"time somebody changed them. The pleasures of prison life " +
					"I guess!\n\n" +
					"Press R to Return to roaming your cell";
		if (Input.GetKeyDown(KeyCode.R)) {myState = States.cell;}
	}
	void state_sheets_1() {
		text.text = "Holding a mirror in your hand doesn't make the sheets look " +
					"any better. \n\n" +
                    "Press R to Return to roaming your cell";
		if (Input.GetKeyDown(KeyCode.R)) {myState = States.cell_mirror;}
	}
	void state_lock_0() {
		text.text = "This is one of those button locks. You have no idea what the " +
					"combination is. You wish you could somehow see where the dirty " +
					"fingerprints were, maybe that would help.\n\n" +
					"Press R to Return to roaming your cell";
		if (Input.GetKeyDown(KeyCode.R)) {myState = States.cell;}
	}
	void state_lock_1() {
		text.text = "You carefully put the mirror through the bars, and turn it round " +
					"so you can see the lock. You can just make out fingerprints around " +
					"the buttons. You press the dirty buttons, and hear a click.\n\n" +
					"Press O to Open, or R to Return to your cell";
		if (Input.GetKeyDown(KeyCode.O)) {myState = States.freedom;}
		else if (Input.GetKeyDown(KeyCode.R)) {myState = States.cell_mirror;}
	}
	void state_cell_mirror() {
		text.text = "You are still in your cell, and you STILL want to escape! There are " +
					"some dirty sheets on the bed, a mark where the mirror was, " +
					"and that pesky door is still there, and firmly locked!\n\n" +
					"Press S to view Sheets, or L to view Lock";
		if (Input.GetKeyDown(KeyCode.S)) {myState = States.sheets_1;}
		else if (Input.GetKeyDown(KeyCode.L)) {myState = States.lock_1;}
	}
	void state_freedom() {
		text.text = "You are FREE!\n\n" +
					"Press P to Play again";
		if (Input.GetKeyDown(KeyCode.P)) {myState = States.cell;}
	}
}

I basically just copied the story line and wrote the code myself I even downloaded the script and tried reviewing everyrthing it seems fine I cant figure this out, im new to coding anything helps thanks.

1 Like

Hey, it usually shows which line is the problem when you double click it in the console

1 Like

Hey thanks for the tip but I think its in the Text inspector heres a screenshot I cant figure it out somethings wrong with the script

1 Like

ohh…
You forgot to set the reference to the text under the Start () method:
you should add:

	text = gameObject.GetComponent<Text>();

this should fix it

1 Like

In the script?

1 Like

yes, in the script

1 Like

Omg thank you so much such a stupid mistake thanks for all the help guys! I’m stupid haha :slight_smile:

1 Like

once bitten twice shy :slight_smile:
no such thing as the S word, ive been there done it sooooo many times.
Thats what makes a community, sharing the stumbles we all made before to help others.

Its just one to watch out for, so when you create a script (if your using the right click, create script method) rename it there and then before opening for editing, that way your class name is fine.

The rest of the script seems fine at first glance, but going forward…

Another common gotcha that rises up a bit is case sensitivity…
namely the Start, Update, FixedUpdate etc methods
these have to be capitalised correctly, or they wont get called. it wont throw an error, they just wont work…

1 Like

Thanks for the help haha I was stuck on this for like three days ugh I’m glad I got it working now I can move on :slight_smile:

2 Likes

Hey @Vandalism,
I’m curious, what was the error?

1 Like

[copy of my original reply post - seems I edited the wrong one late last night, posting here again just for reference]

Generally if you get ‘a reference behaviour is missing’ showing in the inspector, is to do with your class as it cannot load the monobehaviour.

the script filename, must be named the same as the class name, its the way that Unity serialises/loads the script i believe.

if your case the file name is ‘TextControler.cs’ and your class name has two 'll’s

public class TextController : MonoBehaviour {

rename the script filename to EXACTLY mirror the script class ‘TextController.cs’.
remove the script from the object, and then re add it.

should be ok then going forward :wink:

2 Likes

Awesome!! oh, It’s good to know that this error of reference behaviour is missing is because of it, I’ll keep a eye whenever I get this error too =D

Thank you for re posting it, it is good to have the answer for anyone who browses around too, or to someone searching the forum looking to fix the same kind of problem =D

I’ve made this same mistake a couple of times @Vandalism, and probably most people also have, don’t worry :+1::joy:

1 Like