- Home /
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!
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.
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.
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.
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.
Having to set i
to a temporary variable in this instance follows the intended behavior of delegates and closures, not a bug.
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.
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?
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.
Problem is, the user probably don't know how to create an event.
Your answer
Follow this Question
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