Buttons not workling

After finishing the course on the singleton scorekeeper the score recording was working as it should but all the game menu buttons stopped working.

I can start the game in the main game and play and when I die the buttons work. But when I start the game on the main menu screen the buttons do not work, though they did prior to the finishing the singleton scorekeeper lesson.

1 Like

Hi,

Is there still an Event System in the scene where the buttons do not work? Do the buttons interact with your mouse? Are you able to log a message into your console on button click? Are there any error messages in your console in the concerning scene(s)?

Hi:

Sorry for the delay getting back, I was working on another project.

I get this error in the console:

NullReferenceException: Object reference not set to an instance of an object
LevelManager.LoadGame () (at Assets/Scripts/LevelManager.cs:18)

This is the script lines, with 18 being the reset score. I’m assuming when I start the game from the main menu I’m getting the error because there is no score to reset, but I’m not sure that is the issue and am I the only one having this issue?

public void LoadGame()  
{
    scoreKeeper.ResetScore();
    SceneManager.LoadScene("MainGame");
}

Where/how does an object get assigned to the scoreKeeper variable? By default, that variable is null.

Is the ScoreKeeper class a “singleton”? If so, check if your “singleton” code calls gameObject.SetActive(false); in the same if-block where Destroy(gameObject); gets called. If there isn’t that line of code, add it.

I had the same issue. It worked for me. I already put ScoreKeeper Prefab on each scene.

I followed the steps for the buttons on the Level Manager video at 5:34 and it fixed the issue.

Make sure to put the prefab Level Manager on the buttons in the OnClick() section.

Then set the function to its respective level manager command.

Set up for the buttons is correct as in the video. I can click on the quit button and it will give me the debug message. But the Start button still is not working as it should.

Still get the error for here:

public void LoadGame()
{
scoreKeeper.ResetScore();
SceneManager.LoadScene(“MainGame”);
}

Specifically the scoreKepper.ResetScore(); line in the method.

Here is my Level Manager script. I’ve checked it many times to make sure it correctly matches the lesson. But I may be missing something.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.SceneManagement;

public class LevelManager : MonoBehaviour

{

    [SerializeField] float sceneLoadDelay = 2f;

    ScoreKeeper scoreKeeper;

    void Awake()

    {

        scoreKeeper = FindObjectOfType<ScoreKeeper>();

    }

    public void LoadGame()  

    {

        scoreKeeper.ResetScore();

        SceneManager.LoadScene("MainGame");

    }

    public void LoadMainMenu()  

    {

        SceneManager.LoadScene("MainMenu");

    }

    public void LoadGameOver()  

    {

        StartCoroutine(WaitAndLoad("GameOver", sceneLoadDelay));

    }

    public void QuitGame()

    {

        Debug.Log("Quitting Game...");

        Application.Quit();

    }

    IEnumerator WaitAndLoad(string sceneName, float delay)

    {

        yield return new WaitForSeconds(delay);

        SceneManager.LoadScene(sceneName);

    }

}

Here is the Scorekeeper script:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScoreKeeper : MonoBehaviour

{

    int score;
    static ScoreKeeper instance;

   void Awake()

   {
       ManageSingleton();
   }

   void ManageSingleton()

   {
       if (instance != null)

       {
           gameObject.SetActive(false);
           Destroy(gameObject);
       }

       else

       {
           instance = this;          
           DontDestroyOnLoad(gameObject);
       }

   }

    public int GetScore()

    {
        return score;
    }

    public void ModifyScore(int value)

    {
        score += value;
        Mathf.Clamp(score, 0, int.MaxValue);
        Debug.Log(score);
    }

    public void ResetScore()

    {
        score = 0;
    }

}

Maybe I’m missing something but I cannot see any Debug.Logs in the scripts you posted. Where did the messages come from?

Furthermore, could you please format your code with the </> button to make it more readable?

Last but not least, did you check the value of scoreKeeper in Awake? Maybe FindObjectOfType<ScoreKeeper>(); did not return any object. Also try to rename Awake() to Start() in the LevelManager to ensure that the code in Awake() in the ScoreKeeper object gets executed first. Since Unity executes scripts in a random order, it might be that the Awake() method of the LevelManager gets (randomly) executed before the Awake() method of the ScoreKeeper.


See also:

The Debug.Log is in the Level Manager QuitGame method, so when you click on the Quit Game button it prints the message to the console instead of logging out of the game.

Changed Awake to Start, didn’t change anything.

Not sure exactly how to post the scripts, I had in the past took screenshots but was asked to not use that format. This time I copied and pasted the scripts, and not sure what you mean by using the </> button. Let me know the best way to copy my script to the message boards. Thanks

Let me know the best way to copy my script to the message boards.

You did that correctly. You just have to mark the code and click the </> button at the top of the window where you typed your comment. :slight_smile:

image

I have just formatted the code you posted earlier.

Which Awake method did you rename to Start? The Awake method in the ScoreKeeper must not be renamed.

It was in the Level Manager.

Also I can add // to line 18 scoreKeeper.ResetScore(); to comment it out, then the start button works as it should. But then the score doesn’t get reset to 0 after each game or restart.

1 Like

That’s odd. I cannot see anything in the ResetScore method that might be blocking your button. :confused:

Could you please upload your project to GitHub without the Temp and Library folder and share a link to the public repository here? I’d like to take a look into this.

Here is a tutorial video by Brackeys:

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms