- Home /
NullReferenceException: Object reference not set to an instance of an object
i dont understand why im getting this problem, here's my script public class Pause : MonoBehaviour {
public Text label;
public DistanceSystem anotherScript;
// Use this for initialization
void Start () {
}
void Awake(){
anotherScript = GetComponent<DistanceSystem> ();
}
// Update is called once per frame
void Update () {
label.text = anotherScript.distanceTravelled.ToString ("F0");
}
i dragged the text that the distancesystem is attached to it as the public distancesystem anotherscript, and label is the text i want to display the score at distance system looks like this
public Transform player;
public Text label;
public float distanceTravelled;
float lastPosition;
void Start () {
lastPosition = player.transform.position.y;
}
// Update is called once per frame
void Update () {
distanceTravelled = player.transform.position.y - lastPosition;
label.text = distanceTravelled.ToString ("F0")+"m";
}
which works fine, just when i press pause it doesn't print it where i want it and returns the error in the title
You can insert a Debug.Log into the Awake() method:
void Awake(){
anotherScript = GetComponent<DistanceSystem> ();
if (anothierScript == null)
Debug.Log("Awake: Distancesystem not found.");
else
Debug.Log("Awake: Distancesystem found.");
}
Same goes to Update:
void Update () {
if (anothierScript == null)
Debug.Log("Update: Distancesystem not found.");
else
Debug.Log("Update: Distancesystem found.");
label.text = anotherScript.distanceTravelled.ToString ("F0");
}
This shows you if the Distancesystem was found in Awake and is initialized in Update. You can see the result in the Console window (Ctrl-Shift+C).
I think of two things that can go wrong:
your pause script is attached to a game object that does not contain the DistanceSystem as a component.
maybe there is a problem with the initialization order of some classes. Try to move the code from Awake to Start.
Reading questions tagged with NullReferenceException is always a good plan. I'd have closed this as a duplicate if your question didn't already have an answer.
Answer by andyblem · Mar 30, 2015 at 09:46 AM
i will assume your null error exception points you at the code inside your Pause Update function. That error means you are trying to access an object which is not there, which means the anotherScript variable is null. Which leads us back to your awake function. This implies that the initilization statement in the Awake function does not work(it is not capturing anything). You can post the code when you press the pause button to see what you are trying to do
Try the code in Start rather than Awake. The object might not be available at that moment.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Get Distance From Mouse To Canvas BoxCollider 1 Answer
[C#] Can't get variable from other script. Yields NullReferenceException 1 Answer
Adding and removing specific components to a list based OnTriggerEnter/Exit, Getting Null Exceptions 1 Answer