Reputation: 113
I have the following piece of code. I referenced the text component in the editor to no_lives. The gamemanager (singleton) is being instantiated a scene before. The debug.log() shows 5 in the console. But when I try to set the text I get that the reference is not set to an instance of the object. Why is that?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class level1_script : MonoBehaviour {
public Text no_lives;
// Use this for initialization
void Start () {
no_lives = GetComponent<Text>();
}
// Update is called once per frame
void Update () {
int lives_n = gamemanager.lives_f ();
Debug.Log (lives_n);
no_lives.text = lives_n + " x";
}
}
Upvotes: 1
Views: 697
Reputation: 247591
Are you certain that Start()
was called before Update
and that the GetComponent
is actually setting a value to no_lives
?
If not then code defensively. Also lives_n
needs to be converted to a string lives_n.ToString() + " x"
or use a format.
void Update () {
int lives_n = gamemanager.lives_f ();
Debug.Log (lives_n);
if(no_lives != null) {
no_lives.text = string.Format("{0} x", lives_n);
}
}
EDIT:
But when I try to set the text I get that the reference is not set to an instance of the object.
That's because when you do GetComponent<Text>()
, it will look for the instance of Text
component on the-same GameObject your level1_script
script is attached to.
You have two choices:
1.Attach Text
component that GameObject your level1_script
script is atached to then your current code should work.
2.If you already have the Text
component attached to another GameObject, let's say an Object called "MyText", use GameObject.Find to find "MyText" then perform GetComponent
on it to get the Text
component.
no_lives = GameObject.Find("MyText").GetComponent<Text>()
Edit:
Well, both Text and script are attached to the same object prntscr.com/f7p7vx
It's likely attached to multiple GameObject and the other GameObject does not have the Text
script attached to it.
Select the level1_script
script, go to Assets --> Find References in Scene then remove the duplicated script from other objects.
Upvotes: 3
Reputation: 131
As the script logs '5' in the debug log, it seems to me that the variable lives_n is used correctly. My guess is that the component "no_lives" isn't filled (that's what is often causing the "reference is not set to an instance of the object" error) I suggest that you try to add the script to the UI Text object in your scene, this way the script finds the "Text" component on that object.
It's important to know the differences between objects and components, because they're both essential parts of unity. Objects are "things" in your scene, like cubes, cameras and stuff like that. Components on the other hand are part of object and define the properties of that object. For example: a camera object has a camera component to make it a camera and to make it actually viewable by the user. The GetComponent<>() method searches for a (in your case) Text component on the object the script is attached to.
Hope this helps! (oh and don't forget to remove the script from the other object you've placed it on, in case this works. Otherwise you'll still get errors)
Upvotes: 1