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. Dismiss Notice

Resolved Object reference not set to an instance of an object (very strange)

Discussion in 'Scripting' started by Slain1337, Jun 22, 2023.

  1. Slain1337

    Slain1337

    Joined:
    Jun 22, 2023
    Posts:
    2
    I'm new to the engine but this error looks really strange and I can't understand, where I am wrong
    Here's the code
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Enemymovement : MonoBehaviour
    4. {
    5.     public float speed = 10f;
    6.  
    7.     private Transform target;
    8.     private int wavepointIndex = 0;
    9.  
    10.     void Start()
    11.     {
    12.         target = waypoints.points[0];
    13.     }
    14.  
    15.     void Update ()
    16.     {
    17.         Vector3 dir = target.position - transform.position;
    18.         transform.Translate(speed * Time.deltaTime * dir.normalized, Space.World);
    19.     }
    20. }
    Error is in 17 line
    NullReferenceException: Object reference not set to an instance of an object
    Enemymovement.Update ()

    Code of waypoints script in case it is something wrong there

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class waypoints : MonoBehaviour
    4. {
    5.     public static Transform[] points;
    6.  
    7.     void Awake ()
    8.     {
    9.         points = new Transform[transform.childCount];
    10.         for (int i = 0; i < points.Length; i++)
    11.         {
    12.             points[i] = transform.GetChild(i);
    13.         }
    14.     }
    15. }
    16.  
    I suppose that I made stupid mistake but I still can't find it
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,588
    It's confusing that you named your waypoints class in lower case. I was looking for a variable before i continued reading and saw you post the class contents.

    Anyways. You set target to points[0]. You get a NullReferenceException. So the content of that is null. We know what is null, now figure out why! This can have various reasons, from code not being executed, to GetChild(0) returning null. Use Debug.Log to print information and gain additional insights.

    We also have a forum sticky post about Nullreferenceexceptions in this subforum. Check it out. One of the most common exceptions and solving it is always the same! :)
     
    Slain1337 and flashframe like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,558
    It does not look even remotely strange. It is a bog-standard nullref. Go fix it.

    As Yoreki already hinted at above:

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that

    You never need to post for this error.
     
  4. Slain1337

    Slain1337

    Joined:
    Jun 22, 2023
    Posts:
    2
    You wouldn't believe me, but after closing and opening the project error dissapeared, but thanks for the hints. I'm just starting learning Unity and your advices are really useful
     
  5. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,588
    That's unlikely. Not saying i dont believe you, but before you start believing the Engine may be bugged for future errors, these things are way more likely to be human error than any actual bug in the engine. A restart cant hurt, but usually it's safe to assume the mistake is actually on your end. Which may have been the case here too.. maybe you did something before restarting that fixed the error. Anyways, glad you got it resolved. Feel free to ask again if you have trouble in the future. Til then, happy developing!
     
    Slain1337 likes this.