Is there a way to write this code efficiently?
Im triggering Setactive to true one object at a time. Butat the same time making the rest of the panels false.
If (MyPanel==1)
{
MyPanel01.SetActive=true;
MyPanel02.SetActive=False;
MyPanel03.SetActive=False;
}
If (MyPanel==2)
{
MyPanel01.SetActive=False;
MyPanel02.SetActive=True;
MyPanel03.SetActive=False;
}
So on, as you can see it can get too much if you use tens and hundreds of panels.
Yep, there certainly is a simpler way to do it! It would be extremely tedious if there wasn’t. Here’s a little bit of code that can accomplish what you want I think:
// Just drag in panels into this list in the inspector to populate it
public List<GameObject> panels = new List<GameObject>();
public void ActivatePanel(int index) {
// Make sure that index isn't invalid
if (index < 0 || index >= panels.Count)
return;
// Go through each panel
for (int i = 0; i < panels.Count; i++) {
// If i == index (if i is the panel index we are looking for) set to true, else false
panels*.SetActive(i == index);*
} } Just put the panels in the list that is shown in the inspector, and whenever you call ActivatePanel, give it the number of the panel you want activated (starting at 0). It will set all panels to inactive but the one you want. Hopefully that helps!
I tried it but the panel don’t appear when I cusomize it like this index=GameController.instance.level
Level is a number coming from GameController instance
And call everything through
Void Start()
{
ActivePanel(index)
}