- Home /
Question by
IlkoIliev · Sep 14, 2015 at 10:36 AM ·
uiunity5toggletoggle button
Adding Listener to Toggle
Hello there, I have been trying for a couple of hours to add listeners via code to my toggle buttons in a toggle group. Here it is what I do:
public Toggle mainMenuToggle;
public Toggle friendsToggle;
public Toggle leaderboardsToggle;
void Start()
{
mainMenuToggle.onValueChanged.AddListener((value) => MainMenuToggle(value));
friendsToggle.onValueChanged.AddListener ((value) => FriendsMenuToggle(value));
leaderboardsToggle.onValueChanged.AddListener ((value) => LeaderBoardsMenuToggle(value));
}
public void MainMenuToggle(bool isOn)
{
Debug.Log ("called");
if (isOn)
{
Debug.Log ("MainMenu On");
}
else
{
Debug.Log ("MainMenu Off");
}
}
public void FriendsMenuToggle(bool isOn)
{
if (isOn)
{
Debug.Log ("Friends On");
}
else
{
Debug.Log ("Friends Off");
}
}
public void LeaderBoardsMenuToggle(bool isOn)
{
if (isOn)
{
Debug.Log ("Leaderboard On");
}
else
{
Debug.Log ("Leaderboard Off");
}
}
The functions don't seem to hook up to the toggles. I tried to hook them up manually in the editor but there is confusion with the boolean parameter so I wan to that in code.
Thank you for your help in advance!
Regards, Ilko!
Comment
Answer by mykhailov-ihor · Jul 06, 2016 at 10:25 AM
friendsToggle.onValueChanged.AddListener ((bool value) => FriendsMenuToggle(friendsToggle.isOn));
Simpler way :
friendsToggle.onValueChanged.AddListener ( Friends$$anonymous$$enuToggle );
Your answer
Follow this Question
Related Questions
Dropdown with multiple active options/choices(toggles)? 0 Answers
Do Something ONLY when all Toggles are On or Off 4 Answers
Access UI.Toggle from c# CodeBehind -How does one set the Toggle created in the front end of Unity? 1 Answer
UI Toggle OnValueChanged sometimes forces me to select an input. 1 Answer