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 siddharth3322 · Dec 03, 2015 at 03:49 PM · uiunity 4.6toggletoggle button

Toggle Group event called 2 times Unity UI

Why my toggle group event called 2 times on click of Toggle button?

I have created just test scene for this purpose and getting same result.

Here is my inspector image :

alt text

Following is Toggle component image :

alt text

I have written following code in script :

 public void OnTankTypeToggleClick (int tankType)
     {
         Debug.Log ("click method called : " + tankType);
     }

Above debug statement is print on console two times. By this mean, click method is called two time. I want to call click method just single time. So please give some awesome suggestion to me.

screen-shot-2015-12-03-at-91534-pm.png (12.6 kB)
screen-shot-2015-12-03-at-91552-pm.png (38.2 kB)
Comment
Add comment · Show 12
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 siddharth3322 · Dec 04, 2015 at 06:24 AM 0
Share

I need some suggestions for this. Please someone help me in this.

avatar image siddharth3322 · Dec 04, 2015 at 08:45 AM 0
Share

Okay, here are some more details regarding requirement.

Following image represent raycast details :

alt text

Code for script is above only. Just click method exist in full script. I just pass index over each button click argument.

I am getting following output when I click on 1 number toggle.

alt text

screen-shot-2015-12-04-at-20505-pm.png (51.4 kB)
screen-shot-2015-12-04-at-21432-pm.png (14.7 kB)
avatar image saschandroid siddharth3322 · Dec 04, 2015 at 09:14 AM 1
Share

If you have a toogle group and only one member can be active at a time, switching a toggle "on" makes another toggle go "off". So you have 2 OnValueChanged events and your script on the main camera is called twice (by the toggle script you pressed "on" and the one which is going "off" automatically).

avatar image siddharth3322 saschandroid · Dec 04, 2015 at 09:43 AM 0
Share

@saschandroid, thanks for your reply. You are right about this. One event is called for "off" and other is called for "on" state. I just want single call then what to do?

Show more comments
avatar image Nidhii · Jul 06, 2017 at 01:01 PM 0
Share

Did you find any solution to this problem?

avatar image Bonfire-Boy Nidhii · Jul 13, 2017 at 08:04 AM 0
Share

See the comments above.

If you want the toggles to be independent then they shouldn't be in a toggle group.

If you don't want them to be independent then you probably want the function to be called on both toggles, but to do different things depending on whether the toggle has been turned on or off. You can do this by checking the value of the toggle in the callback function and acting accordingly.

avatar image Necrohunter · Jun 18, 2020 at 05:07 PM 0
Share

anyone know a solution? i have this problem always when i use toggle groups. i saw some time ago someone wrote its a bug within unity, is there some workaround? or some fix i dont know about?

avatar image siddharth3322 Necrohunter · Jun 18, 2020 at 05:09 PM 0
Share

That is because on toggle states become OFF and other toggle states become ON. Because of this reason, two times same event gets called.

avatar image Bonfire-Boy Necrohunter · Jun 18, 2020 at 05:26 PM 0
Share

I believe the solution is in the comments above. It's not a bug: as explained above the function will be called for each button. In a toggle group, some toggles will be turned off when anther is turned on - so they all get a callback.

Exactly how you want to handle this fact will depend on exactly you want to achieve.

avatar image Bonfire-Boy Bonfire-Boy · Jun 18, 2020 at 05:30 PM 1
Share

Also have converted to comment.

Answers are for answers :)

If the above doesn't help, you're probably best off posting a new question with details of your situation and details of what you think is going wrong.

avatar image Necrohunter Necrohunter · Jun 18, 2020 at 08:42 PM 0
Share

if(GetComponent().isOn == true)

i just checked it this way. man it was a struggle. i had some other problems as well, but this worked..

1 Reply

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

Answer by siddharth3322 · Jun 18, 2020 at 05:30 PM

This problem, I was facing because one toggle state becomes OFF and another toggle state becomes ON. So for these both events, the same method will get called.

This is the reason why the same method called two times. This is not a bug but by the choice implementation. You have to put one check condition within the method code so it can only get called one time.

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

45 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 avatar image avatar image avatar image

Related Questions

Dropdown with multiple active options/choices(toggles)? 0 Answers

Adding Listener to Toggle 1 Answer

Do Something ONLY when all Toggles are On or Off 4 Answers

UI Toggle OnValueChanged sometimes forces me to select an input. 1 Answer

Toggles/Toggle Groups Question 0 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