Why cant i enabled text with text.enabled?


I am trying to enable Text which is disabled by default.

But for some reason the following code does not work:

GameObject.Find(key).transform.GetChild(0).transform.GetComponent<Button>().interactable = false;
Text over = GameObject.Find (key).transform.GetChild (1).transform.GetComponent<Text>();
over.enabled = true;

However Button is set to interactable successfully.
What can be the reason for this?

Thank you.


According to the image, Text component is already enabled.

I think you meant to activate (enable) the GameObject that the Text component resides in.

If that’s the case, you can do this:

 Text over = GameObject.Find (key).transform.GetChild (1).transform.GetComponent<Text>();
 //over.enabled = true;


GameObject.Find (key).transform.GetChild (1).gameObject.SetActive(true);

So yeah, not to confuse the two, but you can have inactive GameObjects with enabled components,

GameObject.find is very slow. If you want controll the “Text” gameobject in MenuController you can declare variable Text and drag your Text gameobject into it (See Tags and Layers for more infomation). So in your MenuController will have something like this:

 public Text txtComplete;
    void OnCompleteLevel() //just example
          txtComplete.enable = true; // if you disable Text component default or
          txtComplete.gameobject.SetActive(true);  // if you disable GameObject default

But if have another reason you cannot declare txtComplete variable, you just can find “Text” at runtime you can set “Tag” to the Text game object and use Text txtComplete = GameObject.FindGameObjectWithTag(“tagName”).GetComponent();
this help?