Enable/Disable 31 Objects with buttons

Hello, i know how i can do this the long and hard way but im asking for help to make it easier and or better.

So i have 31 game objects that act as Button Backgrounds, i want to be able to click on one button to enable that buttons BGObject and disable all the other BG game objects from the other buttons and when i click on the second does the same.

I was planing on doing an Array and just write all of the 31 Elements, making 31 functions for this…

I was thinking about a for loop for this but my understanding of for loops is not that great.

Any help will be greatly appreciated.

Once you have your array, you can create a function that takes an index number, and uses that to determine which ones to enable/disable. And then the Buttons can be set to call that function with that number as the parameter.

public GameObject[] allObjects; //assign in inspector
public void SetButton(int activeIndex) {
for (int b=0;b<allObjects.Length;b++) {
allObject[b].SetActive(b == activeIndex);
}
}

You may be able to simplify setup of this (e.g. not have to drag 31 things into the inspector) by having a script on each object, and using GetComponentsInChildren to populate the array, but I’d have to have more context to know how to advise you on setting that up.

1 Like

Worked Like a Charm!!!

Thank you so much!

I’m glad you got that working. Since I was reading this anyways, to see if it was resolved, I was just curious:
Is there only ever one BG object active at a time? :slight_smile:

Yes it only activates 1 object while it disabled the other ones, also using it to hide and unhide different “sections”.

Cool :slight_smile: I had an alternative suggestion, back at the time of writing my post here, and it also happened to be very similar to something I had talked with another user about… However, since it’s all working… who cares lol. Have fun with your project :slight_smile: