Iterate through gameobject names to set several similar named active / inactive

Hello everybody,

although I suppose this is an easy one for someone whom is not an autodidact :wink: I could not find the right terms to google to find an answer.

Situation:
I do have several game objects referenced in a class
public GameObject btn_video_0;
public GameObject btn_video_1;
public GameObject btn_video_2;

Goal:
I would like to set some of them active, always starting with 0 but ending at several different increments which are defined by the length of an array.

Current Idea:
I thought the easiest would be to go through a for loop depending on the length of that array, but for obvious reasons this does not work.

for (int i = 0; i < array.Length; i++) {
    btn_video_[i].SetActive(true);
}

Do you have any ideas?

Instead of having several named variables like:

public GameObject btn_video_0;
public GameObject btn_video_1;
public GameObject btn_video_2;

You should just have one array instead:

public GameObject[] videoButtons;

Then you can do your loop:

for (int i = 0; i < videoButtons.Length; i++) {
    videoButtons[i].SetActive(true);
}

Everything that Praetor says is spot on.

Another way is to parent them all to a single GameObject and turn that on/off all at once, if they are fixed in advance.

@PraetorBlue A great many thanks for the quick and helpfull answer, this worked like a charm :slight_smile:

@Kurt-Dekker unfortunately I need some of them active and some not, otherwise your route would be the easiest for sure, so thank you too :wink: