- Home /
Null Reference Exception question, might have something to do with EZGUI (C#)
Okay, so I'm kind of new to coding in Unity, and I keep getting a null reference exception error in one of my programs. I'm working with EZGUI, and I'm basically trying to change the text on one of my labels based on which button is clicked on the menu. Any help would be greatly appreciated; I'm at a loss here.
Here's the code:
SpriteText operatorLabel;
MainMenuManager operatorData;
// Use this for initialization
void Start ()
{
operatorLabel = GameObject.Find ("Label Operator").GetComponent<SpriteText>();
//and here's the error:
operatorData = GameObject.Find ("Main Menu Manager").GetComponent<MainMenuManager>();
SetOperator(operatorData.newOperator);
}
void SetOperator(string newOperator)
{
operatorLabel.Text = newOperator;
}
Where does the null reference say it is occurring? Should give you script name and line number...
$$anonymous$$ost likely is a failure of GameObject.Find() to find the object. Verify that the spelling and capitalization is identical between the code above and the named game object. Evan an extra space at the end of the name will cause this method to fail. Also make sure you are not mixing up names and tags.
Answer by appearance · Feb 06, 2013 at 07:54 AM
GameObject.Find ("Main Menu Manager") is the culprit here.
As robertbu said, verify the spelling and case. If "Main Menu Manager" object is in the root you can do something like below to check if it finds or not:
GameObject goMainMenuManager = GameObject.Find ("/Main Menu Manager");
// GameObject goMainMenuManager = GameObject.Find ("Main Menu Manager");
if (goMainMenuManager == null) {
Debug.LogError ("Where the hell is my Main Menu Manager game object???");
} else {
operatorData = goMainMenuManager.GetComponent <MainMenuManager> ();
}
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Yet another null reference exception 1 Answer
NullReferenceException error in an array of objects 0 Answers
Can't find source of trigger Null Reference Exception 1 Answer