- Home /
NullReferenceException: Object reference not set to an instance of an object?
I have a really annoying bug in my code and I can't for the life of me figure out whats wrong with it. any help will be much appreciated, Thank you in advance. please appreciate that I am very new to unity scripting. Heres my script...
#pragma strict
var isDead : boolean;
var showDeadGUI : boolean;
var canMove : boolean;
var lastPositionY : float = 0f;
var fallDistance : float = 0f;
var player : Transform;
private var controller : CharacterController;
var currentHealth : float = 10.0f;
function Start () {
controller = GameObject.Find("First Person Controller").GetComponent(CharacterController); //require the first person controller script
isDead = false; //is the player dead?
showDeadGUI = false; //should be asking to restart?
canMove = true; //can the player move?
}
function Update () {
if(lastPositionY > player.transform.position.y) //last y position > player y
{
fallDistance += lastPositionY - player.transform.position.y; //if the last y position is more or equel to the fall distance change the players y position
}
lastPositionY = player.transform.position.y; //sets the last y position
if(fallDistance >= 5 && controller.isGrounded) //defines the fall height
{
currentHealth -= 5; //takes 5 away from the players current health
ApplyNormal(); //applys a normal statement
}
if(fallDistance <= 5 && controller.isGrounded) //if fall distance is less than 5 apply a normal
{
ApplyNormal(); //applys a normal
}
if(currentHealth <= 0) //if the players health is less than or the same as 0 set isDead to true
{
isDead = true; //sets is dead to true
}
if (isDead == true) //is isDead true?
{
showDeadGUI = true; //should the code be showing Dead GUI
canMove = false; //can the player move?
{if (Input.GetKeyDown (KeyCode.Return)) { //if player wants to restart press enter
Application.LoadLevel (0); //restart the game, the first level
showDeadGUI = false; //once enter pressed stop showing dead GUI
}
}
}
if (canMove == false) //if the game is over we need to tell the script to freeze the game
{
Time.timeScale = 0.00; //sets the speed of the game to 0 FPS
if (Input.GetKeyDown (KeyCode.Return)) //is the return key down?
{
Time.timeScale = 1.00; //sets the game to full playback rate
}
}
}
function ApplyNormal() //applys a normal
{
fallDistance = 0; //defines the fall distance
lastPositionY = 0; //defines the fall height
}
function OnGUI() //defines a GUI function
{
GUI.Box(Rect(10, 20, 100, 20),"" + currentHealth); //draws a box with current health
if (isDead == false) //isDead false?
{
GUI.color = Color.red; //sets the color to red if false
}else
{
GUI.color = Color.green; //sets the color to green if true
}
GUI.Box(Rect(10, 40, 100, 20),"" + isDead); //draws the isDead GUI box
if (showDeadGUI == true)
{
GUI.backgroundColor = Color.red; //sets the dead GUIs background to red
GUI.color = Color.red; //sets the text color to white
GUI.Box(Rect(100, 400, 1000, 100),"" + "Press Enter To Restart. Don't feel bad that you died just press enter and pretend that it never happend"); //draws the dead GUI box and asks for a restart
}
}
When you get a bug, double click on the error in Unity and it will take you to the problem line of code.
At a guess, I wonder if you have a Game Object in your hierarchy called "First Person Controller"...?
Answer by Bunny83 · Oct 20, 2014 at 02:58 AM
Such an errormessage is always paired with a stacktrace and a linenumber which tells you exactly where in your code the null-ref-exception has beed thrown. You should always include the line number and tell us which line it actually is (since the line numbers here on UA don't have to match the linenumbers in your file)
Anyways, you have two potential variables which can cause problems: "player" and "controller".
So if player is null then you simply forgot to set it up in the inspector. You have to drag your player to the "player" variable.
If controller is null there are a few possible reasons. Either there is a GameObject called "First Person Controller" but it doesn't have a CharacterController, or there isn't such an object at all. In the second case you will get a null-ref exception in Start as well. In the first case you will get an error each Update which will terminate your Update at this point each frame.
Again it would help to know the exact error message and the linenumber. Also when the error occures (once (in Start?), each frame?, "only when i do ...")
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
What should I learn first for Javascript? 2 Answers
java to C# 2 Answers
get a height of a gameobject 1 Answer
How to make a selection system? 1 Answer