Search code examples
c#arraysunity-game-enginegameobject

Can a Unity GameObject Array be used to emulate ToggleGroup like behavior


This app has panels with collections of buttons which are on "Play" disabled. The goal is to utilize a GameOject array to manage these buttons so that only one button can ever be enabled at a time. In an ToggleGroup, it is possible to force only one Toggle object to be selected at any time.

Though growing in Unity c# skills, i readily admit to being more beginner than intermediate in skills. So, any guidance is most appreciated.

The first step was to test the use of ToggleGroups, but Toggles cannot accept the c# script associated with the buttons, which, when enabled, loads a Prefab from an AssetBundle.

Once ToggleGroups were taken off the table, i have been researching the use of an array of GameObjects. Several sources show how in the simplistic manner, to create an array of buttons, and upon Start(), make the buttons inactive. But i am unclear how to emulate the ToggleGroup functionality in an array.

Here is the base of my "buttonMgtArray" script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class buttonMgtArray : MonoBehaviour
{

    public GameObject[] button;

    // Start is called before the first frame update
    void Start()
    {
        button[0].SetActive(false);
        button[1].SetActive(false);
        button[2].SetActive(false);
    }

}

There are no errors at this time because i am attempting to identify how to emulate the desired behavior in the context of GameObject Array.

To better illustrate the behavior i am attempting to invoke, this screencap may help explain the goal.

Button Behavior Explanation

I am very pleased to report that @Stanley had a very elegant solution that provides the toggling emulation. The key was to apply the script to the button that enabled the hidden button, which are the array references. Here is the final code. Thanks much to @Stanley

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class buttonMgtArray : MonoBehaviour
{
    public GameObject[] buttonArray;

    public void DisableAllButtonsExcept(int i)
    {
        foreach (GameObject buttonGameObject in buttonArray)
        {
            buttonGameObject.SetActive(false);
        }
        buttonArray[i].SetActive(true);
    }

}

Solution

  • IF i understand you correctly you want to use your buttons like radio buttons. I see that no one has answered your question yet so i'll give it a quick go.

    There are more ways of doing this and better ways, but this one does work.

    public void DisableAllButtonsExcept(int i) {
        foreach(GameObject buttonGameObject in buttonArray) {
            buttonGameObject.SetActive(false);
        }
        buttonArray[i].SetActive(true);
    }
    

    before Before running code


    after After running code

    Button number 2 is kept because arrays start at 0

    enter image description here

    change the parameters in the onClick event to match its position in the array