Variable not actually assigned?

I’m sorry, I know something like this has been asked about a billion times, but I haven’t managed to find a version of the question that seems to match my circumstances, so here’s to a billion and one.

Short version: I’m creating instances of a prefab with a custom “UIButton” class assigned to it. I’m declaring variables for the class at the top as normal, and assigning them in Start(). This works, and I can use those variables in the Start method, but when I call another method for that instance I get the “variable has not been assigned” error.

Long version: I have a simple class that gets assigned to a button prefab:

public class UIButton : UIElement {
    // Our pieces
    [SerializeField]
    GameObject upIndicator;

	// Use this for initialization
    void Start () {
      upIndicator = transform.Find("Up").gameObject;
      upIndicator.SetActive(false);
    }

    public void ActivateUp() {
        upIndicator.SetActive(true);
    }

}

(I’ve simplified it a bit for the question, but that’s the gist)

The prefab consists of a button with an extra image object added to it, named “Up”. The plan was to have “up” available by default from the prefab, but automatically turned off on initialization, ready to be reactivated on instances that need it.

So I create several instances of my button, and for each one of them the Start() function properly finds the child “up” image, assigns that GameObject reference to the “upIndicator” variable, and then sets its active to false. This works great.

But when I get to the instance that actually wants to use the up indicator I call the ActivateUp() function and the script errors out claiming that the variable upIndicator has not been assigned. It was assigned, it worked long enough to deactivate it, and it’s showing up properly in the inspector as being assigned to the “Up” image object child of that instance, but as far as the function is concerned it is null.

The issue exists even if I set that variable to public. I tried changing the code to use this.upIndicator, even though it shouldn’t need it, but that didn’t work. I’ve even tried reversing the logic of the thing completely, on the off chance that it was losing the variable assignments after setting the gameObject to inactive.

I’m wondering if it is somehow losing the reference to the correct UIButton instance, but it doesn’t seem like it would be. I even tried it on a direct call:

        UIButton newButton = Instantiate(buttonPrefab).GetComponent<UIButton>();
        newButton.ActivateUp();

Even with that my up indicator is being turned off on instantiation, never to be seen again.

I have been racking my brain trying to sort this out, so any help would be greatly appreciated.

Why you use the find function? I mean, the UP object is a child of the instantiated object, correct? The why do not you reference to that object from the inspector? Prefabs that refer to objects in the prefab, do not lose that reference and will be instantiated too. So… drag the prefab in the scene, reference the UP object at the inspector and you click Apply to save the prefab. You will be fine! Make you life easier :slight_smile: