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...

–

It's line 11 on this one

–

Most 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.

–

1 Answer

1

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> ();
    }