I
I
Ilya2021-12-25 12:45:55
Unity
Ilya, 2021-12-25 12:45:55

What is the correct way to write onClick(clicked) in Unity?

onClick code doesn't work and I'm attaching an error

public class NewBehaviourScript : MonoBehaviour
{
    private Button button;
    // Start is called before the first frame update
    void Start()
    {
    button=GetComponent<Button>();
       button.clicked.AddListener(setDifficulty);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    void setDifficulty()
    {
        Debug.Log(gameObject.name + "clicked");
    }
}

61c6e8473e2f7456898569.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Farawa, 2021-12-25
@Farawa

Button.onClick.addlistener(func);
public void func(){}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question