Evans Belloeil
Evans Belloeil

Reputation: 2503

How to have exclusive toggle in unity?

I can't find a solution that fit my needs, so here's my problem.

I've download an Asset that gives my lots of Ready Made Component, I use 8 Toggles, but they have to be exclusive, here's chat I have in my editor :

enter image description here

First I was thinking to attach a script to each toggle, and use a function that turn off every button when the trigger onAction is lauchend on a toggle, then turn on only the toggle that launch the trigger. But it won't work because turning off a button launch is onAction trigger too, and so I go in an infinite loop.

Is there a better working way to do it ?

Upvotes: 0

Views: 1419

Answers (3)

paul p
paul p

Reputation: 450

public class Topic : MonoBehaviour
{

    public ToggleGroup Topiz;
      Toggle maybe1;

    // Use this for initialization
    void Start () 
    {

    }


    // Update is called once per frame
    void Update () 
    {
        maybe1 = Topiz.ActiveToggles().FirstOrDefault();

    }

}

Upvotes: 0

joreldraw
joreldraw

Reputation: 1736

You need to use ToogleGroup to only 1 can be switched on at a time. Check the documentation here

Upvotes: 2

Adi
Adi

Reputation: 103

You can create a class that contains all the toggles (GameObject.Find(name)) as members and when you get onAction call on a toggle script just tell the class which toggle got pressed.. Then in the class you update the gui and control all the toggles

Upvotes: 0

Related Questions