Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by haim96 · Jan 14, 2015 at 04:27 PM · uibuttontoggledelegate

add delegate to toggle in unity 4.6 UI

i know i can set action "Onclick" for button with delegate. so how do i apply delegate for the toggle component? i don't have OnClick for it. only onValueChanged. i'm adding toggles to panel with script and need to set each toggle the relevant index for "updateVehicleDisplay".

so i tried:

 toggleScript.toggle.onValueChanged.AddListener(delegate {updateVehicleDisplay(i);});

and i have:

 public void updateVehicleDisplay( int  index){
         Debug.Log (vehicleList[index].name);
     }

i don't get compile error, but nothing is happen when i click the toggle.

thanks!

Comment
Add comment · Show 9
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 Mmmpies · Jan 14, 2015 at 04:40 PM 0
Share

Flying blind a bit as I can't access unity AT$$anonymous$$ do you have a toggle group set?

I just Add Component on an empty gameObject and add the group to that.

avatar image haim96 · Jan 14, 2015 at 05:40 PM 0
Share

Thanks but it not relevant to my issue....

avatar image haim96 · Jan 15, 2015 at 01:25 PM 0
Share

any one else have an idea about this?

avatar image Mmmpies · Jan 15, 2015 at 02:02 PM 0
Share

Sorry I thought you'd tried implementing my solution on your code, do you mean you tried my code as is and have issue with it? If so what?

What do you mean by nothing happened when you clicked the toggle? They toggled on and off for me, although they are just independent toggles not part of a group.

avatar image Mmmpies · Jan 15, 2015 at 02:17 PM 1
Share

Did you create the tempToggle as well? The issue isn't to do with the i value itself its to do with how C# stores any copy within that loop so to get round it you need to create a temp int and a temp copy of the gameObject and then pass the tempToggle the tempInt value.

And I appreciate what @brianturner said that this is expected behavior but it seems very counter intuitive to me, guess there's a reason for it though.

EDIT:

Shouldn't that be:

 Toggle temptoggle = temp.GetComponent<Toggle>();

not

 Toggle temptoggle = toggleGO.GetComponent<Toggle>();

EDIT2:

I'm not near a PC with unity so I can only do "thinking" fixes. Can you check if the int passed to updateVehicleDisplay matches the current selected in the toggle group then select that vehicle and if it doesn't deselect it, or just ignore it altogether.

$$anonymous$$ust be a better way than that but can't test anything at the moment. I'll have a look tonight when I get back to a Unity PC if you don't find a fix by then.

Show more comments

2 Replies

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

Answer by Mmmpies · Jan 14, 2015 at 06:09 PM

Sorry @haim96 being an idiot and trying to answer things on my phone when I can't even read the full post.

Got this working with this code:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class DelegateToggle : MonoBehaviour {
 
     public GameObject prefabToggle;
     public RectTransform ParentPanel2;
 
     // Use this for initialization
     void Start () {
 
         for(int i = 0; i < 5; i++)
         {
             GameObject goToggle = (GameObject)Instantiate(prefabToggle);
             goToggle.transform.SetParent(ParentPanel2, false);
             goToggle.transform.localScale = new Vector3(1, 1, 1);
             
             Toggle tempToggle = goToggle.GetComponent<Toggle>();
             int tempInt = i;
             
             tempToggle.onValueChanged.AddListener(delegate {ToggleClicked(tempInt);});
         }
     
     }
     
     // Update is called once per frame
     void ToggleClicked (int toggleNo) {
         Debug.Log ("Toggle changed = " + toggleNo);
     }
 }

There's an issue with C# holding the temp value i on loops that I believe is fixed in 5 but not 4.6. If you set a temp int to hold i it works. That's probably your issue but I can get it to output which number was clicked with the code above.

EDIT:

Not sure, my code works but you will have to set the parent panel to have a Vertical layout Group and untick force expand at least on height. Then add a layout element to the prefab toggle and set the minimum height to something like 30.

Create a test canvas and put my code on there, drag in a toggle prefab and parent panel to the script, hey you've got 2k karam! I don't need to tell this :¬) all I'm saying is test my code as is and see what results you get.

Comment
Add comment · Show 5 · 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
avatar image steakpinball · Jan 14, 2015 at 09:34 PM 0
Share

Having to set i to a temporary variable in this instance follows the intended behavior of delegates and closures, not a bug.

avatar image Mmmpies · Jan 14, 2015 at 09:48 PM 0
Share

Well I didn't say it was a bug just that it was an issue. I've read this doesn't happen in unity 5. Is that right? Genuinely interested, not trying to start a fight :-)

I came across this for lambdas but delegates and lambdas are pretty similar so I thought it might help.

avatar image haim96 · Jan 15, 2015 at 08:49 AM 0
Share

O$$anonymous$$, tried it and got odd results. it print the index number while adding the toggles to the panel. but nothing happen later when i click on the toggles.... any idea?

avatar image haim96 · Jan 15, 2015 at 02:43 PM 0
Share

well done, thanks!!

avatar image efge · Sep 23, 2015 at 12:22 PM 0
Share

Still works in 5.1.2
Thanks!

avatar image
0

Answer by Jignesh G. · Jan 16, 2015 at 07:12 PM

Instead of creating delegate most simplest and easiest way would be to create an event, you can register event in any class and it will be executed when when your desired event occurs.

Comment
Add comment · Show 1 · 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
avatar image asperatology · Oct 22, 2015 at 08:50 PM 0
Share

Problem is, the user probably don't know how to create an event.

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

29 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

Related Questions

UI Button Pressed color motion don't reset to Normal color 1 Answer

Runtime Click Listener Addition 0 Answers

UI toggle.onValueChanged assigning method via script 1 Answer

Toggle Button Question 0 Answers

Unity UI dynamic Buttons 4 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