Unity gives me NullReferenceException: Object reference not set to an instance of an object... why?

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class NumberWizard : MonoBehaviour {

	
	int max;
	int min;
	int guess;
	public string tries = "Number of tries left is: ";
	public int maxGuessesAllowed = 9;
	
	public Text triesies;
	public Text text;
	
	void Start () {
		triesies.text = (tries + maxGuessesAllowed);//here is the problem, the code goes off but it keeps on telling me the Object reference not set to an instance of an object
		StartGame ();

	}
	
	void StartGame () {
		max = 1000;
		min = 1;
		guess = 500;
		max = max + 1;
	}
	

	
	public void GuessHigher(){
		min = guess;
		NextGuess ();
	}
	public void GuessLower (){
		max = guess;
		NextGuess ();
	}
	//public void ThatsIt(){
	
	//}
	void NextGuess () {	
		guess = (max + min) / 2;
		text.text = guess.ToString();
		maxGuessesAllowed = maxGuessesAllowed - 1;
		triesies.text = (tries + maxGuessesAllowed);
		if(maxGuessesAllowed <= 0){
		Application.LoadLevel("Win");
		}
	}
}

Hi,

Check that you have dragged your Text UI GameObject into the exposed field in your script component, triesies.

Also, check out the link below regarding formatting of lasted code :slight_smile:


See also;

Privacy & Terms