Search Unity

Button.OnClick not working (trying to change scene)

Discussion in 'Editor & General Support' started by wiklandersandra, Aug 17, 2019.

  1. wiklandersandra

    wiklandersandra

    Joined:
    Jul 29, 2019
    Posts:
    4
    I can't get this to work on one of my scenes, it's supposed to act as a main menu to be able to start the actual game. I have never managed to get the SceneManager.LoadScene function to work, but in this case it seems to not even work to click on the actual button. It's just the default Unity UI button with nothing changed except the text and the fact that I have added the script above (and I am of course referencing to the button in the inspector)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class MenuController : MonoBehaviour
    8. {
    9.     public Button StartButton;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.        
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.        StartButton.onClick.AddListener(StartGame);
    21.     }
    22.  
    23.     void StartGame()
    24.     {
    25.         Debug.Log("Working?");
    26.         SceneManager.LoadScene(1);
    27.     }
    28. }
    29.  
    I tried adding the onClick method to the button in Unity itself first but that didn't work either, so that's why I tried writing the script too. As I said, using "onClick" on button works perfectly fine in my other scene.. I am clueless.