Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by KingMuffin... · Oct 20, 2014 at 12:14 AM · javascript

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Habitablaba · Oct 20, 2014 at 01:20 AM 0
Share

So... what's wrong with it

avatar image richyrich · Oct 20, 2014 at 02:01 AM 0
Share

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"...?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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 ...")

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges