0

I working through an online lesson and getting a Null reference exception. I know it's an extremely common error but neither I or the online help for the course have been able to figure it out. So I'm hoping for some insight here.

Level Manager Script:

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

public class LevelManager : MonoBehaviour
{
    [SerializeField] float sceneLoadDelay = 2f;
    ScoreKeeper scoreKeeper;

    void Start()
    {
        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);

    }
}

Score Keeper Script:

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 = 1;
    }

}

Error is in the level manager at this line: scoreKeeper.ResetScore(); I should add that the a Level Manager and a Score Keeper Object were created with the scripts attached and are in each scene.

Ruzihm
  • 19,749
  • 5
  • 36
  • 48
  • 2
    Suggested reading: [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Jun 15 '22 at 01:16

2 Answers2

0

In the Hierarchy look for at least 2 GameObjects present or not

  • One with ScoreKeeper.cs attached
  • Another with LevelManager.cs attached

If this is ok then open LevelManager.cs and add private keyword before the line ScoreKeeper scoreKeeper; as you are already finding and attaching it in Start() method, like this below

private ScoreKeeper scoreKeeper;
sujoybyte
  • 584
  • 4
  • 19
  • The objects with LevelMananger and ScoreKeeper are prefabed and in each scene. Still get the error after adding private. – Jonathan Meyer Jun 16 '22 at 04:22
  • I don't know your scene set up so, If possible tell me a bit elaborately. One thing I am assuming you are creating these objects with the scripts at runtime is it ? – sujoybyte Jun 16 '22 at 10:34
0

The issue is that you need to call ScoreKeeper.instance.ResetScore(). You have it setup as a singleton. Otherwise you will need to add a concrete reference to an instance in your level manager.

You also need to make the instance public:

public static ScoreKeeper instance;
KingOfHypocrites
  • 9,316
  • 9
  • 47
  • 69
  • Tried adding .instance but get this error: 'ScoreKeeper.instance' is inaccessible due to its protection level [Assembly-CSharp]csharp(CS0122 – Jonathan Meyer Jun 16 '22 at 04:23
  • You need to make the instance public. See my updated code above. – KingOfHypocrites Jun 18 '22 at 01:18
  • Made the changes but still getting the null reference. :-( – Jonathan Meyer Jun 19 '22 at 05:26
  • Make sure you have both the level manager and score keeper scripts added ONCE to a gameobject on the SAME scene. Make sure that the Scorekeeper occurs FIRST in the heirarchy just to rule out timing issues. Also note that you won't be able to load a scene and reference objects from the previous scene, so make sure that's not happening as well. Lastly, put a breakpoint in the start method and make sure the reference is getting set. – KingOfHypocrites Jun 19 '22 at 14:50
  • You will also want to put a breakpoint in the ManageSingleton method to make sure the instance reference isn't getting wiped out. Being that you had a few typos already (missing the public keyword for example)... you might be better better off redoing the exercise and then posting any remaining issues. It could simply be another typo that is messing up the code. – KingOfHypocrites Jun 19 '22 at 15:05