Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

A little help with UI.Toggle

Discussion in 'Scripting' started by Ian094, Jan 24, 2016.

  1. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Hey,

    I'm having a simple but frustrating problem using toggles.

    Basically, I have 2 toggles. I want to make it so that when toggle A is on, toggle B should be off and vice versa.

    I've tried linking the following code to the UI toggles but it crashes Unity for some odd reason :
    Code (CSharp):
    1. //Linked to ToggleA
    2. public void ToggleA(){
    3.         toggleA.isOn = true;
    4.         toggleB.isOn = false;
    5.     }
    6.    
    7. //Linked to ToggleB
    8.     public void ToggleB(){
    9.         toggleA.isOn = false;
    10.         toggleB.isOn = true;
    11.     }
    12.    
    I would really appreciate any help.

    Thanks.
     
  2. manlaikin1994

    manlaikin1994

    Joined:
    May 15, 2015
    Posts:
    179
    Code (CSharp):
    1. public class NewBehaviourScript : MonoBehaviour
    2. {
    3.     public Toggle toggleA;
    4.     public Toggle toggleB;
    5.  
    6.     public void ToggleA(bool b)
    7.     {
    8.         toggleA.isOn = !b;
    9.         toggleB.isOn = b;
    10.     }
    11. }
    I tried it with a simple button and it works
     
    Last edited: Jan 24, 2016
  3. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Hey, thanks for your reply.

    I just tried that but Unity still crashes.

    I'm not using a button, I'm using toggles. When I click the toggle, my project crashes.

    I'll probably just have to use button clicks instead of toggle clicks to achieve what I'm trying to do.
     
  4. manlaikin1994

    manlaikin1994

    Joined:
    May 15, 2015
    Posts:
    179
    I success finally!!!!

    What i guess is an infinite loop problems

    There are some tricky points:

    "The on value changed(Bollean) in Toggle is not as same as button's on value changed.

    According to the api,
    "A UnityEvent that is invoked when the Toggle is clicked. The event can send the current state as a bool type dynamic argument."

    which means Toggle will change his value if we click and if you are using scripts to change the "values : isOn", it will change the value more than 1 time.

    so i use eventTrigger componment to do it.

    For my scripts, i am change the other toggle value only.
    and the toggle that i click right just now will change his value automatically

    The package is attached. You can check it :)
     

    Attached Files:

    Last edited: Jan 24, 2016
    Ian094 likes this.
  5. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    It works! Thanks :)

    I had to make a few tweaks to get exactly what I want but everything's working great now.

    Thanks again.