Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

NullReferenceException: object reference not set to an instance of an object

Discussion in 'Scripting' started by ChrysalisCloud_, Nov 29, 2015.

  1. ChrysalisCloud_

    ChrysalisCloud_

    Joined:
    Nov 20, 2015
    Posts:
    1
    I'm pretty basic at C# and I'm trying to create player and it's physics properties and I get an error when I try to debug the program and I need help. Here are my scripts:

    Player:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player : MonoBehaviour {
    5.  
    6.     public float speed = 50f;
    7.     public float jumpPower = 150f;
    8.     public float maxSpeed = 3;
    9.  
    10.     public bool grounded;
    11.  
    12.     private Rigidbody2D rb2d;
    13.     private Animator anim;
    14.  
    15.     void Start ()
    16.     {
    17.  
    18.         rb2d = gameObject.GetComponent<Rigidbody2D>();
    19.         anim = gameObject.GetComponent<Animator>();
    20.     }
    21.    
    22.     void Update ()
    23.     {
    24.  
    25.         anim.SetBool("grounded", grounded);
    26.         anim.SetFloat("speed", Mathf.Abs(Input.GetAxis("Horizontal")));
    27.  
    28.     }
    29.  
    30.     void FixedUpdate()
    31.     {
    32.  
    33.         //moving the player
    34.  
    35.         float h = Input.GetAxis("Horizontal");
    36.  
    37.         rb2d.AddForce((Vector2.right * speed) * h);
    38.  
    39.         //limiting the speed of player
    40.  
    41.         if (rb2d.velocity.x > maxSpeed)
    42.         {
    43.             rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
    44.         }
    45.  
    46.         if(rb2d.velocity.x < -maxSpeed)
    47.         {
    48.             rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
    49.         }
    50.  
    51.     }
    52. }
    53.  
    GroundCheck:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GroundCheck : MonoBehaviour {
    5.  
    6.     private Player player;
    7.  
    8.     void Start ()
    9.     {
    10.  
    11.         player = gameObject.GetComponent<Player>();
    12.  
    13.     }
    14.    
    15.     void OnTriggerEnter2D (Collider2D col)
    16.     {
    17.  
    18.         player.grounded = true;
    19.  
    20.     }
    21.  
    22.     void OnTriggerExit2D (Collider2D col)
    23.     {
    24.  
    25.         player.grounded = false;
    26.  
    27.     }
    28. }
    29.  
    Here is the error: "NullReferenceException: object reference not set to an instance of an object. GroundCheck.OnTriggerEnter2D (UnityEngine.Collider2d col) (at Assets/Scripts/GroundCheck.cs:18)"

    Please help me with this error ASAP.
     
  2. Fra123X

    Fra123X

    Joined:
    Mar 10, 2013
    Posts:
    40
    "NullReferenceException: object reference not set to an instance of an object. GroundCheck.OnTriggerEnter2D (UnityEngine.Collider2d col) (at Assets/Scripts/GroundCheck.cs:18)"

    So the exception was thrown inside the "GroundCheck" script's "OnTriggerEnter2D" function, and the line is 18, which is "player.grounded = true".
    The error is saying that player is not instantiated, which probably refers to:

    Code (CSharp):
    1. player = gameObject.GetComponent <Player> (); //player = GetComponent<Player>();
    That "Player" component is probably not in the same GameObject as the GroundCheck script.
    Make sure you didn't put this script somewhere else by mistake, and that the Player script is on the same gameobject as the GroundCkeck one and not in a child gameobject or a parent.