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. Dismiss Notice

Toggle Button Bug?

Discussion in 'UGUI & TextMesh Pro' started by Winklehner, Sep 2, 2014.

  1. Winklehner

    Winklehner

    Joined:
    Apr 12, 2013
    Posts:
    20
    Hi, I'm observing a strange behaviour with the toggle button (Unity4.6b18).

    I can activate a toggle button with toggle.isOn = true but never deactivate it again. toggle.isOn = false
    just has no effect.

    Is this a bug?

    Thomas
     
  2. Caio_Lib

    Caio_Lib

    Joined:
    Mar 4, 2014
    Posts:
    83
    Hi Thomas,
    I'm using Unity 4.6 b18 too, this code works for me:

    Code (CSharp):
    1.  
    2. void Start()
    3. {
    4.     var toggle = GetComponent<Toggle>();
    5.  
    6.     if (toggle != null)
    7.         toggle.isOn = false;
    8. }
    9.  
    Could you show your code?
     
  3. Panzerhandschuh

    Panzerhandschuh

    Joined:
    Dec 4, 2012
    Posts:
    17
    Using a toggle group will prevent you from setting isOn to false. You can bypass that by doing something like:
    Code (csharp):
    1. Toggle toggle = GetComponent<Toggle>();
    2. ToggleGroup group = toggle.group;
    3. toggle.group = null;
    4. toggle.isOn = false;
    5. toggle.group = group;
     
  4. Winklehner

    Winklehner

    Joined:
    Apr 12, 2013
    Posts:
    20
    Thats it! My toggle is part of a toggle group and this prevents setting toggle.isOn = false again.
    Is this expected behaviour?
     
  5. Panzerhandschuh

    Panzerhandschuh

    Joined:
    Dec 4, 2012
    Posts:
    17
    I think it is expected behavior since toggle groups prevent buttons from being toggled off when you try to click the buttons. It would be nice to have some kinda option to avoid this though.
     
    rakkarage likes this.
  6. elliselkins

    elliselkins

    Joined:
    Mar 14, 2014
    Posts:
    6
    There is an option on togglegroup component to "Allow Switch Off". If checked then the toggle that is on can be toggled off, otherwise it can't.
     
    sebagork likes this.