Search Unity

NullReferenceException: Object not set to an instance of an object

Discussion in 'Scripting' started by PelleFixar1, Mar 13, 2019.

  1. PelleFixar1

    PelleFixar1

    Joined:
    Jan 24, 2019
    Posts:
    6
    I am trying to create a tower defense game but I'm having a problem (as you probably expected)

    I don't know a lot about scripting and coding, I watch YouTube videos to learn how to do certain things, and I'm following one right now for the path system of the enemy. I can't find what the problem is, so I'm turning to you guys to show me the way.

    Line 22 is where the error is.

    The full code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Enemy : MonoBehaviour {
    6.  
    7.     GameObject pathGO;
    8.  
    9.     Transform targetPathNode;
    10.     int pathNodeIndex = 0;
    11.  
    12.     public float EnemySpeed = 0f;
    13.     public int EnemyHealth = 1;
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.         pathGO = GameObject.Find("Path");
    18.     }
    19.  
    20.     void GetNextPathNode()
    21.     {
    22.         targetPathNode = pathGO.transform.GetChild(pathNodeIndex);
    23.         pathNodeIndex++;
    24.     }
    25.    
    26.     // Update is called once per frame
    27.     void Update () {
    28.  
    29.         if (targetPathNode == null) //If it's not already going towards a node, it sure is now
    30.         {
    31.             GetNextPathNode();
    32.  
    33.             if(targetPathNode == null) //If it goes null again, that means that it made it through the course and must be ELIMINATED
    34.             {
    35.                 ReachedGoal();
    36.             }
    37.         }
    38.  
    39.         Vector3 dir = targetPathNode.position - this.transform.localPosition;
    40.  
    41.         float distOfFrame = EnemySpeed * Time.deltaTime;
    42.  
    43.         if(dir.magnitude <= distOfFrame)
    44.         {
    45.             //Here we check if an enemy is at a node or something idk
    46.             targetPathNode = null;
    47.         }
    48.  
    49.         else
    50.         {
    51.             //If it isn't, then just F***ing go there idiot enemy
    52.             transform.Translate(dir.normalized * distOfFrame, Space.World);
    53.             Quaternion targetRotation = Quaternion.LookRotation(dir);
    54.             this.transform.rotation = Quaternion.Lerp(this.transform.rotation, targetRotation, Time.deltaTime * 5);
    55.         }
    56.  
    57.     }
    58.  
    59.     void ReachedGoal()
    60.     {
    61.         Destroy(gameObject);
    62.     }
    63.  
    64. }
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Does it find the object on start?
    Add a Debug.Log and see.
     
  3. PelleFixar1

    PelleFixar1

    Joined:
    Jan 24, 2019
    Posts:
    6
    It just says " The type name 'Log' does not exist in the type 'Debug' "
     
  4. Simpso

    Simpso

    Joined:
    Apr 20, 2015
    Posts:
    158
    It's basically saying it either can't find the Path gameobjext or a gameobjext doesn't exist as a child of Path.

    In the scene hierarchy do you have a game object called Path ? With a capital p?
    Does the Path object have a child under it that has a transform component on it?
     
  5. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    How did you write it?
     
  6. PelleFixar1

    PelleFixar1

    Joined:
    Jan 24, 2019
    Posts:
    6
    Debug.Log;
    Was that wrong?
     
  7. PelleFixar1

    PelleFixar1

    Joined:
    Jan 24, 2019
    Posts:
    6
    This is my hierarchy
     

    Attached Files:

    • yet.PNG
      yet.PNG
      File size:
      11.2 KB
      Views:
      540
  8. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Code (CSharp):
    1.     void Start () {
    2.         pathGO = GameObject.Find("Path");
    3. //Debug.Log("Put your string here");
    4. if(pathGO == null){
    5. Debug.Log("pathGO not found!");
    6. } else{
    7. Debug.Log("pathGO" found!");
    8. }
    9.    }
    10.  
     
  9. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    You don't have an object named "path" it's not gonna find nothing.
     
  10. PelleFixar1

    PelleFixar1

    Joined:
    Jan 24, 2019
    Posts:
    6
    Okay so I changed the "path" to PathNode and now that error doesn't show up anymore, but now I get another error saying "UnityException: Transform child out of bounds", and it's highlighting the same line.

    I googled it and apparently it has to do with my script not checking if the pathnode has any children or something like that. Do you know how to fix that?
     
  11. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    You're trying to find the 'pathNodeIndex' child of PathNode, it doesn't have any - thus the null ref.

    why not just make an array of game objects/ transforms and have them there instead of doing the all the find stuff?
     
  12. PelleFixar1

    PelleFixar1

    Joined:
    Jan 24, 2019
    Posts:
    6
    Thanks for the help and the tip! I'll look into it :)