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

GameObject.Find
only returns active gameobjects. If your panel is currently not active in hierarchy it won’t find it.

You could try adding a public GameObject, assigning it in the inspector, then doing what you need to in the code.