Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

I´m new and need help with my script ( NullReferenceException)

Discussion in 'Scripting' started by Super3DSpace, Sep 26, 2019.

  1. Super3DSpace

    Super3DSpace

    Joined:
    Aug 20, 2019
    Posts:
    3
    Hi guys,

    i´m new to the Forum and Unity, so please forgive me if this was asked before.

    I keep gettin the following error(however my game runs without any problems):
    NullReferenceException: Object reference not set to an instance of an object
    Patrol.Update () (at Assets/TheGame/Scripts/Patrol.cs:57)


    It referes to this script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Patrol : MonoBehaviour
    6. {
    7.     public float speed =-3f;
    8.     //variable to check if the object is moving right
    9.     private bool movingRight = true;
    10.  
    11.     //creates a Transform in wich we have to put our GroundDetection gameobject in the Inpector
    12.     public Transform GroundDetection;
    13.     //gives acces to the Playercollision script
    14.     private PlayerCollision playercollision;
    15.     private bool isDestroyed = false;
    16.     Collider2D o_collider;
    17.  
    18.     private void Start()
    19.     {
    20.         //initializes the playercollisin pointer
    21.         playercollision = GameObject.FindObjectOfType<PlayerCollision>();
    22.         o_collider = GetComponent<Collider2D>();
    23.     }
    24.  
    25.     private void Update()
    26.     {
    27.         //tells the Animator to play the death animation, if isDestroyed is true
    28.         GetComponent<Animator>().SetBool("destroyed",isDestroyed);
    29.         //checks if the enemy was destroyed
    30.        
    31.         //moves the enemy position right times speed
    32.         transform.Translate(Vector2.right * speed * Time.deltaTime);
    33.         //output is saved into groundInfo ; creates a Ray from the position of GroundDetection, downwards with the lenght of 2 f
    34.         RaycastHit2D groundInfo = Physics2D.Raycast(GroundDetection.position,Vector2.down,2f);
    35.         //if the ray hits nothing
    36.         if (groundInfo.collider ==false )
    37.         {
    38.             //if the object is moving right
    39.             if (movingRight == true)
    40.             {  
    41.                 //we turn the object to 180 degrees on the y axes and set movingRight to false
    42.                 transform.eulerAngles = new Vector3(0f, 180f, 0f);
    43.                 movingRight = false;
    44.  
    45.             }
    46.             //if movingRight is false
    47.             else
    48.             {  
    49.                 //we turn the object to 0 degrees on the y axes and set movingRight to true
    50.                 transform.eulerAngles = new Vector3(0f, 0f, 0f);
    51.                 movingRight = true;
    52.             }
    53.         }
    54.  
    55.         //this part checks if the enemy collides with a wall sideways
    56.         RaycastHit2D sideInfo = Physics2D.Raycast(GroundDetection.position, Vector2.left, 0.3f);
    57.         if(sideInfo.collider.tag == "Ground")
    58.         {
    59.             //if the object is moving right
    60.             if (movingRight == true)
    61.             {
    62.                 //we turn the object to 180 degrees on the y axes and set movingRight to false
    63.                 transform.eulerAngles = new Vector3(0f, 180f, 0f);
    64.                 movingRight = false;
    65.  
    66.             }
    67.             //if movingRight is false
    68.             else
    69.             {
    70.                 //we turn the object to 0 degrees on the y axes and set movingRight to true
    71.                 transform.eulerAngles = new Vector3(0f, 0f, 0f);
    72.                 movingRight = true;
    73.             }
    74.         }
    75.     }
    76.     private void OnCollisionEnter2D(Collision2D collision  )
    77.     {
    78.  
    79.         //checks if the enemy collides with the falling player
    80.         if (collision.gameObject.tag == "Player" && playercollision.isFalling)
    81.         {
    82.             //disables the collider
    83.             o_collider.enabled = !o_collider.enabled;
    84.             //sets the "isDestroyed" variable to true
    85.             isDestroyed = true;
    86.             //destroys the enemy after 0.5 seconds
    87.             Destroy(this.gameObject,0.5f);
    88.         }
    89.     }
    90. }
    91.  
    it is a patrol script for a an enemy i build. As mentioned before the error doesnt stop the game from running, but im not sure if this is an issue or not.

    Thanks for any advice :)
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,351
    Line 56 can return null (if raycast didnt hit anything),
    so the next line fails, since there is no collider:
    Code (CSharp):
    1. if(sideInfo.collider.tag == "Ground")
    you could check if its not null, for example:
    Code (CSharp):
    1. if(sideInfo.collider!=null && sideInfo.collider.tag == "Ground")
    docs:
    https://docs.unity3d.com/ScriptReference/RaycastHit2D-collider.html
     
  3. Super3DSpace

    Super3DSpace

    Joined:
    Aug 20, 2019
    Posts:
    3
    Oh i see now. Yeah i forgot how the raycast function actually works.
    That solved it.

    So this woulndt have caused any issue but it still worried me.

    Thanks a lot for the quick answer.
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,616
    Any time you get an error message, even though it may not effect the game running in "play mode" in the editor, it might still cause your build to crash. It's better to always fix the errors when they come up..
     
  5. Super3DSpace

    Super3DSpace

    Joined:
    Aug 20, 2019
    Posts:
    3
    Yeah thought so aswell, thats why i was worried.