Score isn't work

So… I check my code and didn’t find what’s the problem. The score isn’t work, VisualStudio don’t point any error in the code but when I start the game, Unity show me this message:

And the score isn’t updating or even showing. I check if I forgot to do something in Unnity but nothing that I can see, at least.

Here’s the code, equals to Gary’s code (I check the lecture’s project changes)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class UIDisplay : MonoBehaviour
{
[Header(“Health”)]
[SerializeField] Slider healthSlider;
[SerializeField] Health playerHealth;

[Header("Score")]
[SerializeField] TextMeshProUGUI scoreText;
ScoreKeeper scoreKeeper;

void Awake()
{
    scoreKeeper = FindObjectOfType<ScoreKeeper>();
}

void Start()
{
    healthSlider.maxValue = playerHealth.GetHelth();
}

void Update()
{
    healthSlider.value = playerHealth.GetHelth();
    scoreText.text = scoreKeeper.GetScore().ToString("0000000");
}

}

This is a null error, meaning it is not returning the information that is trying to grab.

In this Case Its located inside your scoreKeeper.cs file and in the method of GetScore().
here you should have a return statement.

    public int GetScore()
    {
        return score;
    }

If you want to try and update that or check against that file to see if that is the problem. The error is telling you its on line 30 which is a null error meaning the score returned nothing.
so dive into your scoreKeepr.cs script and have a look whats happening in there.

Here’s my Scorekeeper.cs script:

public class Scorekeeper : MonoBehaviour
{
int score;

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

}

From what I can see, nothing is wrong with the code. Is there something I’m missing?

Ok the code looks right here(unless im being stupid). Have you made sure the scorekeeper.cs is attached to something on the scene i.e an empty game object. if its not attached then the script isnt being called.

This is the first thing the engine is looking for an object of type ScoreKeeper.

void Awake()
{
    scoreKeeper = FindObjectOfType<ScoreKeeper>();
}

After looking at the code for this lecture:

I see there is also the value stored in the health.cs so double check the right variables are set in the inspector and the code is correct in this script.

Hi Terezu,

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. Have you already tried to add Debug.Logs to your code to see what is going on during runtime?

As @LaniganDev pointed out, the code looks for a ScoreKeeper object in the Awake() method. The question is: Does the Find* method return an object? If it doesn’t, there is no active ScoreKeeper object in the scene. If it does return an object, the referenced ScoreKeeper object might get destroyed later. Then the next questions would be: When is ‘later’? And which code destroys the ScoreKeeper object?

So, I really have no ideia of what was happeing, but I tried to delete and rewrite the script and I done this like 3 times changing the name with “scoreKeeper” to “ScoreKeeper” and “scorekeeper” and finally work. I still have no ideia of why wasn’t work, but now it’s working nicelly

1 Like

Problay I wrote the name incorrectly and wasn’t noticed. I have ADHD, so it’s really commun this sttufs happen. Anyaway, now the code is working correctly. Thanks :slight_smile:

Lets Break it down so you can understand refrencing a bit more, sometimes people get confused at the start as both name are the same but do different things.

By declaring ScoreKeeper scoreKeeper; Your saying to the code "hey code i want a variable scoreKeeper this is going to store the refrence to theScoreKeeper component.

FindObjectOfType<ScoreKeeper>() This is a Unity method, This is now going to say I need to go look for an object in the scene of type ScoreKeeper. Once it finds it it will say I found it thats why you have the variable scoreKeeper it will now assign the found refrence to this variable.

scoreText.text = scoreKeeper.GetScore().ToString("0000000");

This is now the use of the variable, scoreKeeper(so since its stored the refrence to ScoreKeeper,you can call the methods that are inside) Which is what you are doing with GetScore() its looking at what we is returned in this method, and giving you the output in this script.ToString is refering to change the value of score which you previously stored into an int (a numeric value) into a string(text).

So DeclareInitializeUse

I hope this helps, for future refrences. @Nina Might have something else to add but I tried my best to break it down without all the code “talk” as this can get confusing when people start talking about refrences and pointers etc.

1 Like

I was’t able to spot any typo in your code. If you called a method with a spelling error in its name, the compiler would have complained, and you wouldn’t have been able to start your game. For this reason, we can assume that the names are correct. :slight_smile:

And if it doesn’t find anything, the returned value is null. null means ‘no object reference’. That’s why you, Terezu, have to check what value the FindObjectOfType method returns because that’s impossible to tell just by reading the code. If an object was found, the code works as described by LaniganDev (unless scoreKeeper loses the reference down the road for some reason).

1 Like

Privacy & Terms