- Home /
Question by
Faniha · May 19, 2015 at 08:50 AM ·
nullreferenceexception
Error, and I cannot figure out how to remove it
NullReferenceException: Object reference not set to an instance of an object GameOverResults.OnGUI () (at Assets/Scripts/GameOverResults.cs:12)
public class GameOverResults : MonoBehaviour {
public void OnGUI() {
//find score variablen fra handattack
GameObject eHand = GameObject.Find ("Hand");
handAttack gHand = eHand.GetComponent<handAttack> ();
string FinalScore = string.Format("{0}", gHand.theScore);
//find time variablen fra guitimer
GameObject eTime = GameObject.Find ("Plate");
guiTimer gTime = eTime.GetComponent<guiTimer> ();
string Finalminutes = Mathf.Floor(gTime.timer / 60).ToString("00");
string Finalseconds = Mathf.Floor(gTime.timer % 60).ToString("00");
string FinalTime = string.Format("Tid {0:0}:{1:00}", Finalminutes, Finalseconds);
GUI.Label(new Rect(595, 8, 20, 20), FinalScore);
GUI.Label(new Rect(605, 8, 20, 20), FinalTime);
}
}
Comment
use Debug.Log to find the missing component. It can be everything that you retrieve from GameObject.Find or GetComponent. i.e. eHand, gHand, eTime, gTime. What exactly is line 12 in your script? The error occurs there.
I think eTime is null, the GameObject "Plate" has not been found in your scene (you should test eTime value before doing operations on it).
could it be because the things I want to get are in another scene?
Best Answer
Answer by Paul-Sinnett · May 19, 2015 at 09:16 AM
Look at line number 12 in your script. That will tell you which object reference (variable) has the null value.
Your answer
