-1

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);
}
Backs
  • 24,430
  • 5
  • 58
  • 85
Tubestorm
  • 1
  • 4

2 Answers2

1

Generally, Find() is never the best approach for anything.

Try to set a variable reference to your OpenSelection, like you did with your panel, then call this variable.

Lotan
  • 4,078
  • 1
  • 12
  • 30
  • 1
    Extensive usage of Unity's `Find` methods seems to be a plague of people new to Unity or programming in general. Looking through questions here, they're used very often. I always wonder - should it be pointed out as a side note when answering, even if the question is about something else? Like: "here's your answer, and by the way you should be doing (...) rather than using `Find`". – BFyre Nov 28 '20 at 12:26
0

GameObject.Find() only returns active GameObject. Here you are trying to find the OpenSelection panel which is not active. That's why the Find() is not finding the OpenSelection panel.

Easwar Chinraj
  • 436
  • 1
  • 4
  • 10