1

I am a relatively new C# coder. I have never had a problem with this before but for some reason when I try to update my score through code. it breaks my game. here is my code.

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

public class ScoreManager : MonoBehaviour
{
public static ScoreManager instance;

[SerializeField] Text score;

int points;

void Start()
{
    if(instance == null)
    {
        instance = this;
    }
    score = GetComponent<Text>();
    points = 0;
}

// Update is called once per frame
void Update()
{
    score.text = points.ToString();
}

public void AddToScore()
{
    points++;
}
}

If I comment out the score.text = points.ToString(); it will work fine. but it won't update.

Can someone help me this please :(. I am using unity, and I have attached the text file to the scoremanager object. The error i get is this:

NullReferenceException: Object reference not set to an instance of an object ScoreManager.Update () (at Assets/Scripts/ScoreManager.cs:27)

Kody Drew
  • 17
  • 3

1 Answers1

2

Your score field is null, when you are calling:

score.text = points.ToString();

Check using debugger the line where it gets initialized.

Here

score = GetComponent<Text>();

Your score variable gets initialized by null value.

And when you try to use a property of score your get NullPointerException.

Make sure you have initialized your score properly before using its properties.

Rafael
  • 420
  • 4
  • 15
  • I'm sorry, but I am not understanding. I feel like this is so easy but its going right over my head. Isn't it initialized at the top? If I wrote int points = 0; that initializes the points integer. the score is being serialized where in unity I drag the text file called 'score' into it. I don't understand how to initialize score any other way. I am setting the score to the integer 'points'. I am sorry if i sound dumb. I promise I will understand after it is explain. I think I am just on the wrong train of thought here. – Kody Drew Jan 25 '20 at 21:24
  • 1
    You call `GetComponent();` and it returns `null` value. I have no experience with unity, so I suppose you are looking to get an ui-component in your code and somehow it returns null. Check this link, here's how to get ui-component: https://answers.unity.com/questions/864840/how-to-access-textscript-component-in-unity.html – Rafael Jan 25 '20 at 21:28
  • ahhhh. In unity when i type score = GetComponent(); it says that the variable i serialized at the top " [SerializeField] Text score;, is going to reference the component of that object as a Text file. So when I drag a file to it in unity, it has to be 'Text' and I can change any aspect within the 'Text' file. – Kody Drew Jan 25 '20 at 21:39
  • so score.Text = points.ToString(); changes the string of the text that is shown in game but doing score instead. Text is saying that within that text file there is a component named 'Text'(sounds confusing but it isn't if you see it) and i can change what the text actually says. so in update I am trying to make it so I can constantly update the text to be how many points I have in the form of a string..... This has always worked but for some reason this isn't. I can paste other files from other games I have created if it makes it easier to understand. – Kody Drew Jan 25 '20 at 21:39