Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by spinnerbox · Dec 25, 2015 at 12:03 PM · unity 5user interfacetoggle button

Unity gets stuck using Toggles like radio buttons

I have this code for setting up game difficulty:

     public void SetEasyDifficulty ()
     {
         gameState.difficulty = Helper.EASY_DIFFICULTY;
         //easyDifficultyToggle.isOn = true;
         mediumDifficultyToggle.isOn = false;
         hardDifficultyToggle.isOn = false;
         gameState.Save();
     }
     
     public void SetMediumDifficulty ()
     {
         gameState.difficulty = Helper.MEDIUM_DIFFICULTY;
         easyDifficultyToggle.isOn = false;
         //mediumDifficultyToggle.isOn = true;
         hardDifficultyToggle.isOn = false;
         gameState.Save();
     }
     
     public void SetHardDifficulty ()
     {
         gameState.difficulty = Helper.HARD_DIFFICULTY;
         easyDifficultyToggle.isOn = false;
         mediumDifficultyToggle.isOn = false;
         //hardDifficultyToggle.isOn = true;
         gameState.Save();
     }

The reason why specific lines are commented is because when uncommented and I press one of the toggles, Unity gets stuck and I have to kill the process in task manager. On the other hand, if those lines stay commented, all works except I need to press twice, the same toggle to enable it :)

What could be the problem here?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jprocha101 · Dec 26, 2015 at 03:05 AM 1
Share

It might help us answer the question if we see the code that is calling those 3 "Set" methods. Unity hangs like that when it enters an endless loop.

avatar image spinnerbox jprocha101 · Dec 28, 2015 at 11:21 AM 0
Share

Yes I though of that. The strange thing is, in whichever framework you work there is always premade radio button component. :)

avatar image saschandroid · Dec 28, 2015 at 11:40 AM 1
Share

Are you using a ToggleGroup? If so, remember that pressing one toggle would cause at least one other toggle to call its function (because of state change). That means if you press the toggle button for 'medium' and the toggle for 'easy' was active, Set$$anonymous$$ediumDifficulty() AND SetEasyDifficulty() would be called. Only if you press the same toggle button the second time no other function is called. So, using a ToggleGroup could be the cause for the problem you described.

avatar image spinnerbox saschandroid · Dec 28, 2015 at 11:55 AM 0
Share

I didn't know there is something called ToggleGroup until you mentioned it. No, there is nothing in the ToggleGroup field in any of the the Toggles. Working on it now.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by spinnerbox · Dec 28, 2015 at 12:26 PM

How did I solved this? There is a ToggleGroup component which can be added to an empty GameObject. So create an empty game object and press the "add component" to add UI -> Toggle Group. Once you have done that select each of your toggles and fill the Toggle Group field with this empty object. This will set up all toggles in one group and once done that, you can use the following code to find which toggle is currently enabled:

 using UnityEngine;
 
 public MyToggleClass:MonoBehaviour
 {
     public GameObject toggleGroupHolder;
 
     ToggleGroup tGroup;
     IEnumerator<Toggle> gameDiffEnum;
     string toggleName = "";
 
     public void SetGameDifficulty ()
     {
         gameDiffEnum = tGroup.ActiveToggles().GetEnumerator();
         gameDiffEnum.MoveNext();
         toggleName = gameDiffEnum.Current.name;
         
         if (toggleName == Helper.EASY_DIFFICULTY_TOGGLE)
         {
             gameState.difficulty = Helper.EASY_DIFFICULTY;
         }
         
         if (toggleName == Helper.MEDIUM_DIFFICULTY_TOGGLE)
         {
             gameState.difficulty = Helper.MEDIUM_DIFFICULTY;
         }
         
         if (toggleName == Helper.HARD_DIFFICULTY_TOGGLE)
         {
             gameState.difficulty = Helper.HARD_DIFFICULTY;
         }
         
         gameState.Save();
     }
 }

Make sure to fill toggleGroupHolder with the empty object we created and set onValueChanged() event handler for each of the toggles to SetGameDifficulty(). SetGameDifficulty() will be called twice :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

42 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Send inputfield entries via email 2 Answers

From an optimization point of view, what optimizes different resources better? Activating/Deactivating UI elements or hiding and showing UI elements using CanvasGroup's alpha? 2 Answers

Changing UI for network chat 1 Answer

UI Click pass through 1 Answer

Toggle Button: different color for ON and OFF state? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges