Object reference not set to an instance of an object

I got an error upon answering all the quiz it only pop-up upon showing the end screen on line
WinScreen.ShowfinalScore () (at Assets/Scripts/WinScreen.cs:24)
GameManager.Update () (at Assets/Scripts/GameManager.cs:28)

WinScreen.cs

{
    [SerializeField] TextMeshProUGUI finalScoreText;
    ScoreKeeper scoreKeeper;
    // Start is called before the first frame update
    void Start()
    {
        scoreKeeper = FindObjectOfType<ScoreKeeper>();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void ShowfinalScore()
    {
       finalScoreText.text = "Congratulations!\n You got a score of " + scoreKeeper.CalculateScore() + "%";   // this is the line 24
    }
}

Gamemanager.cs

Quiz quiz;
    WinScreen winScreen;

    // Start is called before the first frame update
    void Start()
    {
        quiz = FindObjectOfType<Quiz>();
        winScreen = FindObjectOfType<WinScreen>();

        quiz.gameObject.SetActive(true);
        winScreen.gameObject.SetActive(false);
    }

    // Update is called once per frame
    void Update()
    {
        if (quiz.isComplete)
        {
            quiz.gameObject.SetActive(false);
            winScreen.gameObject.SetActive(true);
            winScreen.ShowfinalScore(); // line 28
        }
    }

    public void OnReplayLevel()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
}

Hi,

NullReferenceException means that a reference (“link”) to an instance is missing. Double click on the error message to see to which line in your code it is referring. If you exposed a field in the Inspector, make sure that it’s not empty.


See also:

1 Like

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

Privacy & Terms