Search Unity

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

Discussion in 'Scripting' started by Midashammer, Sep 24, 2019.

  1. Midashammer

    Midashammer

    Joined:
    Sep 9, 2019
    Posts:
    1
    Hello, i started coding a few weeks ago and im are having this issue
    'NullReferenceException: Object reference not set to an instance of an object
    Enemy.Update () (at Assets/Scripts/Enemy.cs:34)'. Im are following a youtube tutorial (portuguese brazilian) and I dont know how to fix this issue. The enemy was suposed to walk in the direction of the player but its stays still and dont move.
    Youtube link:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Enemy : MonoBehaviour
    6. {
    7.     public float maxSpeed;
    8.     public float minHeight, maxHeight;
    9.  
    10.     private float currentSpeed;
    11.     private Rigidbody rb;
    12.     private Animator anim;
    13.     private Transform groundCheck;
    14.     private bool onGround;
    15.     private bool facingRight = false;
    16.     private Transform target;
    17.     private bool isDead = false;
    18.     private float zForce;
    19.     private float walkTimer;
    20.  
    21.     // Start is called before the first frame update
    22.     void Start()
    23.     {
    24.         rb = GetComponent<Rigidbody>();
    25.         anim = GetComponent<Animator>();
    26.         groundCheck = transform.Find("GroundCheck");
    27.         target = FindObjectOfType<Player>().transform;
    28.  
    29.     }
    30.  
    31.     // Update is called once per frame
    32.     void Update()
    33.     {
    34.         onGround = Physics.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
    35.         anim.SetBool("Grounded", onGround);
    36.  
    37.         facingRight = (target.position.x < transform.position.x) ? false : true;
    38.         if (facingRight)
    39.         {
    40.             transform.eulerAngles = new Vector3(0, 180, 0); // se esta virado para a direita, vira pra esquerda
    41.         }
    42.         else  //vira pra direita
    43.         {
    44.             transform.eulerAngles = new Vector3(0, 0, 0);
    45.         }
    46.  
    47.         walkTimer += Time.deltaTime;
    48.     }
    49.  
    50.     private void FixedUpdate()
    51.     {
    52.         if(!isDead)
    53.         {
    54.             Vector3 targetDistance = target.position - transform.position;
    55.             float hForce = targetDistance.x / Mathf.Abs(targetDistance.x); // h force é -1 se o player estiver na esquerda e +1 se na direita movimentaçao
    56.  
    57.             if(walkTimer >= Random.Range(1f,2f))// entre 1 e 2 seg muda a movimentaçao
    58.             {
    59.                 zForce = Random.Range(-1, 2);
    60.                 walkTimer = 0;
    61.             }
    62.  
    63.             if(Mathf.Abs(targetDistance.x) < 1.5f)
    64.             {
    65.                 hForce = 0;
    66.             }
    67.  
    68.             rb.velocity = new Vector3(hForce * currentSpeed, 0, zForce * currentSpeed);
    69.  
    70.             anim.SetFloat("Speed", Mathf.Abs(currentSpeed));
    71.         }
    72.  
    73.         rb.position = new Vector3   // trava o inimigo na camera
    74.         (
    75.                 rb.position.x,
    76.                 rb.position.y,
    77.                 Mathf.Clamp(rb.position.z, minHeight, maxHeight));
    78.     }
    79.     void ResetSpeed()// reseta a velocidade/ idlee chama
    80.     {
    81.         currentSpeed = maxSpeed;
    82.     }
    83. }
    84.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    NullReferenceException means that you've tried to "dig into" something that's null, so the way to figure out the problem is to look at the line the error points to (in this case, line 34), and then look on the left side of every "dot". Almost always, the problem is that one of those things is null.

    On your line 34, the things to the left of dots are:

    Physics (which is a class, and can't be null)
    transform (which is a property that is guaranteed by Unity to never be null)
    groundCheck (which you assign via transform.Find above, which is notoriously vulnerable to things like spelling errors and very slightly misconfigured scene hierarchies)
    LayerMask (which, like Physics, is a class and can't be null)

    One of these things is not like the others - your groundCheck variable must be the one that's null. When it's assigned in line 26, it must not be finding an object named "GroundCheck" as a child of this transform, and as a result, is null.