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

Need Help with (hopefully) Simple Null Reference Exception

Discussion in 'Scripting' started by Alismadia, Oct 11, 2016.

  1. Alismadia

    Alismadia

    Joined:
    Jan 26, 2015
    Posts:
    20
    Alrighty. Here is the scoop. I am building some silly additions to the Unity Space Shooter Tutorial, which I finished following a few weeks ago. I have built the game to be 2D, and I have been adding on all sorts of little features to practice concepts in scripting.

    The addition I am struggling with presently is a gun for the ship. The gun does this:
    1. When player pushes down the 'e' button, it spawns an invisible game object that is the child of the spawner on my ship; this object is tagged 'enemy' but does not have a collider. (and as a child it follows my player around wherever they travel).
    2. As well, the 'e' button triggers a Co-routine which waits for a moment and then spawns the laser bullet in the same location as the invisible game object.
    3. The instantiated laser bullet has a script on it which 'seeks' out game objects tagged as 'enemy'
    4. if the player pushes 'q', it destroys the invisible game object child; thus allowing the laser bullet to seek out the next nearest enemy and destroy it...
    ^^ all of the above works just fine... honestly better than I thought it would. However. Despite the fact that this works, when there are no enemies left on the screen I get the inevitable "NullReferenceException: Object reference not set to an instance of an object"...

    I have tried a couple of things to make this work, but all of my attempts do not prevent this message from arriving once every enemy on screen has been destroyed... Here is the EnemyTracking Script that is throwing the error, the specific error line identified by unity is in bold (any help would be awesome!):

    using UnityEngine;
    using System.Collections;

    public class EnemyTracker : MonoBehaviour
    {
    public float speed;
    Transform enemySeek;
    public GameObject[] enemies;

    void Start()
    {
    if (enemies == null)
    {
    enemies = GameObject.FindGameObjectsWithTag("Enemy");​
    }​
    }

    void Update()
    {
    GetComponent<Rigidbody2D>().velocity = transform.position * speed;
    foreach (GameObject enemy in enemies)
    {
    enemySeek = GameObject.FindGameObjectWithTag("Enemy").transform;
    transform.position = new Vector2(enemySeek.position.x, enemySeek.position.y) * speed;​
    }​
    }
    }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    if there are no enemies left in the scene, trying to get their transforms isn't going to work... also using an array for the enemies when they change in number isn't a great idea since the array doesn't change size. I'd suggest switching to a list and checking the number of elements in the enemies list before trying to do that "foreach" loop (fyi lists have "count", arrays have "length")
     
  3. Alismadia

    Alismadia

    Joined:
    Jan 26, 2015
    Posts:
    20
    Ok, this seems to logistically make sense to me, but I don't have much experience with the implementation of lists and could use a hand...

    Clearly I need to declare the list in the variable section, lets say something like:
    List<int> targets = new List<int>();

    1. From here, how would I properly feed the data I am looking for into this list, would I get rid of the start method entirely? -- assuming that I would be looking for targets during the update method (since they will be changing all the time?)
    2. Also, once I have the script in place for finding the enemies within the Lists 'targets' feed, how do I go about patching that into the foreach loop and find the enemies without using their transform, but rather the transform of the found 'targets'?