NullReferenceException: Object reference not set to an instance of an object
TextController.state_cell () (at Assets/TextController.cs:30)
TextController.Update () (at Assets/TextController.cs:20)
Note that in the code below lines 20 and thirty are both the text.text line in state_cell(). Line 20 is the call to state_cell() and line 30 is the text.text line itself.
I’m also seeing that some of the code is not being colored correctly. I’ve been seeing this behaviour in all of my scripts so far. It is almost like it’s missing something in the ‘using’ declarations at the top. It’s not resolving the public or private declarations and turning them blue which is probably the same issue as the error up above not quite recognizing them.
The code does RUN just fine with some quirks. Note that this is not the whole script, just the script up to the point of the referenced lines with errors.
Does anyone know what the issue is? This code may as well be pasted directly from the course so I’m thinking it’s something in my Monodevelop settings.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
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();
} else if (myState == States.lock_0) {
state_lock_0();
}
}
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, L to view Lock";
if (Input.GetKeyDown(KeyCode.S)) {
myState = States.sheets_0;
} else if (Input.GetKeyDown(KeyCode.L)) {
myState = States.lock_0;
}
}
Adding that one line didn’t seem to do anything. It just changed the error worthy lines down a line.
NullReferenceException: Object reference not set to an instance of an object
TextController.state_cell () (at Assets/TextController.cs:31)
TextController.Update () (at Assets/TextController.cs:21)
I tried to get a screenshot of the Inspector but there’s no way to upload one here.
I definitely did drag the Text object over to the Text field on the TextController script properties as shown in the prior lesson.
I see in the Text object’s Inspector, at the bottom, a section called TextController.
Two fields:
Script = TextController
Text = Text (Text)
I’ve looked everywhere, I can’t find a Error in this code, I’m very sorry about it. It would be good if you could share an screenshot here (there is a button like an HD with an Arrow pointing UP in the text editor here in the forum, you can upload clicking there);
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
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;
text = GetComponent<Text>();
}
// 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.lock_0) {
state_lock_0();
}
}
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, L to view Lock";
if (Input.GetKeyDown(KeyCode.S)) {
myState = States.sheets_0;
} else if (Input.GetKeyDown(KeyCode.L)) {
myState = States.lock_0;
}
}
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_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;
}
}
}
When I removed the public from the text declaration the Text field disappeared from the Text Controller section of the Inspector. I don’t know why?
The error is still the same, though. The code does function completely as you would think. This is why I suspect it’s not anything in the code at all. I think it’s something in the using declarations at the top of the script and something missing from a classpath or such.
Thank you so much for your time, sir. I wish I could buy you a beer.
When I asked you to put the text = GetComponent<Text>() was a way of manually getting the Text component within the GameObject. When you declare something as public, it allows you to access and change it’s value from outside the script, that is why it appears on the inspector too, so you can manually put what you want. When you removed the public prefix from the Text text, the inspector was no longer able to access it (that is why the box disappeared).
I asked you to do this because the error was saying that the object reference was not set to an instance of it. This kind of error usually appears when the engine don’t know which version of the script/component/GameObject you are trying to access (you can have the same script multiple times across the scene, and even the same GameObject too). The modification that I asked you to do was to make sure that the instance of the Text that you were accessing was indeed the one from the same GameObject. But since it didn’t worked I don’t have a clue of what could be causing it, it could be a bug too, have you tried to restart unity?
or perhaps even recreate the gameobject, it is a strange error indeed
No worries, it is a pleasure to help (or at least try to)
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
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;
text = GetComponent<Text>();
}
// 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.lock_0) {
state_lock_0();
}
}
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, L to view Lock";
if (Input.GetKeyDown(KeyCode.S)) {
myState = States.sheets_0;
} else if (Input.GetKeyDown(KeyCode.L)) {
myState = States.lock_0;
}
}
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_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;
}
}
}
And imported it into my old text 101 source code… And it worked flawlessly.
No warnings, no errors, no weird hilighting in visual studio.
Don’t seems like it is a problem within the script. Have you added this same script to another gameObject (that don’t have a text)? It would cause this problem too.
If nothing works, I recommend you to delete this gameObject and create a new one (just add the component of the script that you just created and an text component too)
Thanks for the effort, guys. I think this is something in the IDE someplace. I’ll keep hunting for it but for now might just move on with the course. I really do appreciate your time.