Hello everyone, I am having some issues when attempting to update a scoreboard inside of my game that I created with a canvas text. My script component is attached to the text child of canvas.
I have a variable called scoreText and it updates properly, i have verified this with a Debug.Log. I want the text inside my canvas to update with the new scoreText. However this line generates the error message— NullReferenceException: Object reference not set to an instance of an object Solution. I looked up some tutorial videos here on Unity as well as General Youtube. But I can not seem to figure out what I am missing/added that causes this error. Any help you can provide would be greatly appreciated, thank you in advance.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class GameScore : MonoBehaviour {
public Text text;
public GameObject blueGoalObj;
public GameObject redGoalObj;
public string blueGoal;
public string redGoal;
public string scoreText;
public int redScore = 0;
public int blueScore = 0;
public void Start()
{
text = GetComponent<Text>();
}
//Brings OnCollision information from another script to alter score
public void RaiseScore(string Goal)
{
if (Goal == blueGoalObj.name)
{
blueScore += 1;
SetScore();
}
else if (Goal == redGoalObj.name)
{
redScore += 1;
SetScore();
}
}
//take the score from RaiseScore() and set it as a String value. Then use that string to replace the current score on the text feild inside a canvas
void SetScore()
{
string redScoreString = redScore.ToString();
string blueScoreString = blueScore.ToString();
string scoreText = redScoreString + " / " + blueScoreString;
// Debug successfully updates the score on the Console.
Debug.Log(scoreText);
//NullReferenceException: Object reference not set to an instance of an object Solution
text.text = scoreText;
}
}
Then this script should work. Try to write debug lineif(text==null)Debug.Log("Error"); and see if it triggers before text.text = line.
If it does, that means either Start() not getting called(object not active?) or GetComponent() returning null (there’s no Text component on same GameObject).
If it doesn’t… I don’t see problems with this code, are you sure error is here?
In line 23, Start method, you set the ‘text’ gameobject to ‘text.text’, so when you are in line 51, you are attempting to access ‘text.text.text’ which does not exist. I would suggest removing that line, and changing the ‘text’ variable name to something else such as ‘scoreText’ for readability.
I understand that they are different(I do all my programming in C#), it just adds a lot of confusion. And you just need to remove the ‘text = GetComponent’ because you already grabbed the Text component in line 7, then in line 23 you try and grab the text component of that (error), and then in line 51 you try and and access and change the ‘text’ string on a non-existing component.
I see what you’re saying, but he might not have grabbed it in inspector and then it would be null. It should work if that script is attached to object containing Text component no matter what text variable contains in Editor and that is stated in OP. Your previous post, however, stated a text.text.text reference, and that can never happen in that code.
I would assume that since he is using the Text text as a public variable, he already directly grabbed the UI.Text GameObject through the inspector by drag+drop(which would then require no need for line 23) and it would work correctly.
And I don’t assume as it’s not stated anywhere. However it’s clearly stated that:
Anyway, let’s drop that offtopic and wait for OP answer as I have large suspicion error happens not in this part of code as I don’t know what Solution is. It’s not defined in this context. Error usually goes like this: NullReferenceException: Object reference not set to an instance of an object. Class.Method(parameters) (at path/file.cs: line)
Thank you so much for your replies. Sorry I truncated my error, the full thing 9s -
NullReferenceException: Object reference not set to an instance of an object
GameScore.SetScore () (at Assets/Scripts/GameScore.cs:51)- The reason Text is public is because eventually i would like this script to run off of an empty game object.
As another update I placed null check into my code and it does in fact say that text is null. Even if I instantiate it inside of the same method like so:
void SetScore()
{
text = GetComponent<Text>();
string redScoreString = redScore.ToString();
string blueScoreString = blueScore.ToString();
string scoreText = redScoreString + " / " + blueScoreString;
// Debug successfully updates the score.
Debug.Log(scoreText);
if (text == null) Debug.Log("ErrorT");
//NullReferenceException: Object reference not set to an instance of an object Solution
// text.text = scoreText;
}
At the time of my posting the script component was attached to the Text Object that was a child of the Canvas Object. Removing ‘text = GetComponent’ and setting it in the Unity Component did not change the error.
I believe that in the end you were right and it was in fact an error from another script. I commented out my RaiseScore() Method and changed to SetScore() to FixedUpdate(). The error resolved after that and my Text.text updated properly.
Uncommenting RaiseScore() brought the error back. So instead I moved it inside of the method that was originally calling in from another script and had it update redScore and blueScore from there. This gives me the in game result I had anticipated.
I am still not sure what exactly was causing my error unfortunately. Perhaps I will have to tackle it again when I begin cleaning up my scripts at a later stage.
Thank you so much for the support, especially with the if(text==null)Debug.Log(“Error”); I would not have come to the conclusions I did without it. Also messing with ‘text = GetComponent’ came in handy as well.