Error at Toggles onValueChanged()
Asked Answered
A

1

0

I have a gameobject with an toggle which I want to execute a function on change:

levelItem.GetComponent<Toggle>().onValueChanged.AddListener(SetSelectedLevel(levelList*.Number));*

public void SetSelectedLevel(int index) {

  •  selectedLevel = index;*
    
  • }*
    the levelList*.Number is just an int.*
    I am getting this error on the onValueChange:
    > error CS1502: The best overloaded
    > method match for
    > UnityEngine.Events.UnityEvent.AddListener(UnityEngine.Events.UnityAction)'*_ _*> has some invalid arguments*_ _*aswell as this:*_ _*> error CS1503: Argument #1’ cannot
    > convert void' expression to type*_ _*> UnityEngine.Events.UnityAction’
Antibes answered 3/7 at 8:49 Comment(0)
A
0

This is how the syntax should look like:

levelItem.GetComponent<Toggle>().onValueChanged.AddListener(delegate {
				SetSelectedLevel(levelList[currentItem].Number);
			});

notice the delegate { ... } which has been added inside the AddListener().

Antibes answered 27/11, 2014 at 10:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.