Home c# How to call script methods when clicking on the UI button?

How to call script methods when clicking on the UI button?




 enter image description here

I’m making the main menu for a game based on a YouTube video. After the narrator wrote scripts for the buttons, he began to add them to these buttons. For some reason I cannot do this. I cannot add a start game or an exit. I just don’t have it. The video displays a context menu and the last line titled NewBehaviourScript . I have not it.

Tell me how to add it? I have already searched all YouTube, Google and the unit website, but I could not find a solution to my problem. The script is written in C # .

Answer 1, authority 100%

In the UI buttons, you can assign the OnClick event.

For example, there are buttons:

 enter image description here

In their properties buttons we find OnClick .

 enter image description here

In it you need to select the object on which the script handler is hanging (this can be the button itself, if desired), and then on the right side of this drop-down list select the very script and the desired method from the same script:

 enter image description here


Moreover, the methods must be public . Example:

using UnityEngine;
public class SwitchCameras: MonoBehaviour {
  private void Start () {
  public void Switch () {
    Debug.Log ("Some method was called here");

When a method with arguments appears, you can add this parameter to the button’s OnClick event:

 enter image description here

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions