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

[Resolved] How to un-check a toggle through script?

Discussion in 'Scripting' started by ajsnarr98, Jan 16, 2016.

  1. ajsnarr98

    ajsnarr98

    Joined:
    Jun 15, 2015
    Posts:
    73
    What is the correct way for "checking"/"unchecking" a UI toggle through script? For some reason this way freezes Unity when I test it:
    Code (CSharp):
    1. public Toggle[] toggles;
    2. int previousToggle;
    3.  
    4. void Start()
    5. {
    6.     previousToggle = 0;
    7. }
    8.  
    9. public void OnToggleClick(int toggleNum)
    10. {
    11.     toggles[previousToggle].isOn = false;
    12.     toggles[toggleNum].isOn = true; //in case previous toggle was the one clicked
    13.     previousToggle = toggleNum;
    14. }
    Can anyone help me? Or is this code the correct way of doing it and I have encountered a strange bug?

    Edit: Basically I just want to make it so when one toggle is clicked, all other "toggled on" toggles become toggled off, so no matter what only one toggle's toggle.isOn is true.
     
    Last edited: Jan 16, 2016
  2. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    a toggle is a boolean, a boolean can only have 2 states so if you say myBool = !myBool; that will make it equal the opposite of what it was before.

    "!" means "Not" in most programming langauges. So putting it in front of a bool will give you the opposite, or using "!=" instead of "==" when comparing values it would return true if the 2 values are not equal.
    Code (CSharp):
    1. toggles[toggleNum].isOn = !toggles[togglenum].isOn;
     
  3. ajsnarr98

    ajsnarr98

    Joined:
    Jun 15, 2015
    Posts:
    73
    Thanks for the suggestion, but that doesn't seem to work either. I seems that if I try to set toggle.isOn to any boolean from a script it freezes Unity. I am sure that is is toggle.isOn because if I remove the line where I set it Unity no longer freezes.

    Should I report this as a bug, or is there another way I am supposed to change that value?
     
  4. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,033
    It should work (tested it myself). If it crashes/freezes it usually means there is no object where you think there should be one. If you see the objects in the list in the inspector there's an odd bug.

    Also, you can optimise it a tad by checking if previous and current toggle are the same and simply return ;)
    /OCD
     
  5. ajsnarr98

    ajsnarr98

    Joined:
    Jun 15, 2015
    Posts:
    73
    Unfortunately they do show up as objects in the inspector. What version of Unity are you using? I am currently using 5.2.3f1

    Edit: I will try updating to 5.3.1 and see if that helps
     
  6. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,033
    I'm on 5.3.1p3.
     
  7. ajsnarr98

    ajsnarr98

    Joined:
    Jun 15, 2015
    Posts:
    73
    5.3.1f1 now and still same problem, I will report this as a bug
     
  8. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    hmm just seen your edit in the case case of only wanting 1 toggle to ever be on, what you should do is loop over your Toggle[] in a foreach and set isOn to false. Than outside of the loop set isOn to true for the one you clicked
     
  9. ajsnarr98

    ajsnarr98

    Joined:
    Jun 15, 2015
    Posts:
    73

    That was the first thing I did, the method I have now really should do the same thing, but faster. However, as I said, any time I try to change a toggle.isOn in a script it freezes Unity.
     
  10. ajsnarr98

    ajsnarr98

    Joined:
    Jun 15, 2015
    Posts:
    73
    @orb can you test again on your version but with this scenario: All the toggles are children of a text object (which I use as a title) which is in the content pane in a scroll view. That is the scenario that I have. Maybe that will affect your test?
     
  11. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148

    put the toggles in a toggle group. It'll do this automatically without any scripting.
     
    felixkoessler likes this.
  12. ajsnarr98

    ajsnarr98

    Joined:
    Jun 15, 2015
    Posts:
    73
    To do that, do I add it the toggle group script and remove the toggle? Or keep the toggle and include the toggle group?

    Oh I see, do I create an empty object with a toggle group component and add it to the group part of the toggles? If so, can the object be anything? Like a UI text object?
     
  13. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,033
    Yes, toggle groups will work. I'd forgotten about them, thinking they were containers, but it's much simpler.

    Add a game object, add the Toggle Group component in the inspector and reference this in each toggle you have. No need to stick the groups in the canvas or anything. Parent objects for the toggles don't matter either.
     
    felixkoessler likes this.
  14. ajsnarr98

    ajsnarr98

    Joined:
    Jun 15, 2015
    Posts:
    73
    Thanks! Toggle groups are working perfectly for me! Now I don't have to worry about Unity freezing :)
     
  15. Oblocinski

    Oblocinski

    Joined:
    Nov 25, 2015
    Posts:
    1
    Hey, Unity QA here.
    In the case that you have submitted I have explained to you as to why it froze - changing "isOn" calls for "On Value Changed" event under Toggle component - you are using it to change "isOn" which results in a infinite loop of changing isOn and calling for the change again. Hope this resolves your issue :)
     
  16. ajsnarr98

    ajsnarr98

    Joined:
    Jun 15, 2015
    Posts:
    73
    Thanks Oblocinski, I'll be weary of that in the future. :D
     
    felixkoessler likes this.
  17. BQT

    BQT

    Joined:
    Dec 20, 2018
    Posts:
    6
    Great solution.
    save my day.
     
    felixkoessler likes this.