The question is answered, right answer was accepted
NullReferenceException, Script Error
Hi,
I wrote a script that should manage that a bool in the animater turns true or false if a BoxCollider2D from the Player called "groundCheck" got triggered by the ground or not. After I finished it and added the script it doesn't worked as I wanted to. An error says:"NullReferenceExeption: Object refernce not set to an instance of an object [...]" I don't know how to fox it. The error seems to apear in line 16 and 21. There is another post in this forum which tells how to fix it but I don't understand it.
 using UnityEngine;
 using System.Collections;
 
 public class GroundCheck : MonoBehaviour {
 
     private Player player;
 
     void start()
     {
         player = gameObject.GetComponentsInParent<Player>()[0];
     }
 
 
     void OnTriggerEnter2D(Collider2D col) 
     {
         player.grounded = true;
     }
 
     void OnTriggerExit2D(Collider2D col)
     {
         player.grounded = false;
     }
 
 }
 
              Take a look at some of the other questions and answers tagged with nullreferenceexception.
I tryd the "Try/Catch" $$anonymous$$ethod but it doesn't fixed the problem, here is the code: using UnityEngine; using System.Collections;
 public class GroundCheck : $$anonymous$$onoBehaviour {
 
     private Player player;
 
     void start()
     {
         player = gameObject.GetComponentsInParent<Player>()[0];
     }
 
 
     void OnTriggerEnter2D(Collider2D col) 
     {
         try {
             player.grounded = true;
         }
         catch (NullReferenceException ex) {
             Debug.Log("player.grounded was not set in the inspector")
         }
     }
 
     void OnTriggerExit2D(Collider2D col)
     {
         player.grounded = false;
     }
 
 }
 
                  I also tryd the "Null check" but also it doesn't helped: using UnityEngine; using System.Collections;
 public class GroundCheck : $$anonymous$$onoBehaviour {
 
     private Player player;
 
     void start()
     {
         player = gameObject.GetComponentsInParent<Player>()[0];
     }
 
 
     void OnTriggerEnter2D(Collider2D col) 
     {
         player.grounded = true;
         if (player.grounded) {
             Debug.Log(player.grounded.name);
         } else {
             Debug.Log("")
     }
 
     void OnTriggerExit2D(Collider2D col)
     {
         player.grounded = false;
     }
 
 }
                 Answer by gjf · Sep 12, 2015 at 07:50 PM
player is never getting initialized because the place that you think you're setting it - start() - never gets called. 
change it to Start() and it should fix your problem ;)
Ok, it solved the NullReferenceExcepttion but the bool of the animator still won't turn true if the player is grounded.
Follow this Question
Related Questions
Why do I keep getting this error? 1 Answer
Referencing another script error/problem 0 Answers
2D Top-Down Shooting Script Errors 1 Answer
Unity Overlap Point Causing Error 0 Answers