- Home /
Question by
Tubestorm · Nov 28, 2020 at 03:26 AM ·
uigameobjectcanvasscripting beginnerfind-gameobject
How to find UI in code c# (button, Panel etc)
Hello, in an empty game object i have a selection Manger script. I'm having some difficulties with finding a panel (called: OpenSelection) I created in Canvas. I would like to find the panel where ever it is in the hierarchy and set enabled to true.
But the code isn't finding the panel. I'm not sure why.
any help would be appreciated
//UI
private GameObject panel;
// Start is called before the first frame update
void Start()
{
panel = GameObject.Find("OpenSelection");
panel.SetActive(true);
}
Comment
Answer by hulahoolgames · Nov 28, 2020 at 04:35 AM
GameObject.Find only returns active gameobjects. If your panel is currently not active in hierarchy it won't find it.
Answer by BrainLagsaLot · Nov 29, 2020 at 12:22 AM
You could try adding a public GameObject, assigning it in the inspector, then doing what you need to in the code.