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:
In their properties buttons we find OnClick
.
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:
Everything.
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: