I made a menu, looked through many tutorials and videos, but ran into a problem: I added some buttons to the Canvas and tried to hang a couple of functions on the buttons, but after adding the script, the buttons are not pressed. Source:

using UnityEngine; using UnityEngine.SceneManagement; public class main_menu : MonoBehaviour { public void Start1() { SceneManager.LoadScene("Part1"); Grid easy = new Grid( 100, 10, 10); } public void Start2() { SceneManager.LoadScene("Part2"); Grid Hard = new Grid(100, 10, 10); } public void Exit() { Application.Quit(); } } 

Is it possible that the reason why the buttons are not pressed is another?

  • After adding the script where? Did you select the Onclick event on the buttons and tied the same object with the script and its methods? Can you describe in detail how what was assigned to what? You can even with screenshots)) - Alexey Shimansky

1 answer 1

I prefer to bind listeners somehow. If not hurt your eyes, then you can try.

 public class TestButtonScript : MonoBehaviour { private Button Button { get; set; } private void Start() { Button = GetComponent<Button>(); Button.onClick.AddListener(() => { OnClick(); }); } private void OnClick() { Debug.Log("Click"); } } 
  • I made out, thanks for the help - Alexander Ryabikov