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);
	}
}

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?

–

Yes, it is because it is not in the scene.

–

[46640-facepalm.jpg|46640] ^^ You cannot access objects that are not in your scene .

–

1 Answer

1

Look at line number 12 in your script. That will tell you which object reference (variable) has the null value.