Missile Code needs help

when there is no targets on screen it gives NullReferenceException error… how can i set it to be destroyed if there is no targets on screen? also how can i set it to acquire multiple targets?

using UnityEngine;
using System.Collections;

public class Missile01 : MonoBehaviour
{
    public float Speed;
    public Transform Target;
    public GameObject go;
    private Transform mis;

    void Start () {
        mis = transform;
        }
   
    void Update ()
    {
        go = GameObject.FindGameObjectWithTag ("EnemyShipX");
        Target = go.transform;
        mis.position = mis.position + (mis.forward * Speed * Time.deltaTime);
        mis.LookAt (Target);
    }
    void OnTriggerEnter (Collider asd)
    {
        if (asd.CompareTag("EnemyShipX"))
        {
            Destroy(this.gameObject);
        };
    }
    void DestroyProtocol () {
        Destroy (this.gameObject);
    }
   
}

Well, GameObject.FindGameObjectWithTag has to return null if there is no such object to be found. The very next line of code confidently dereferences this result (go) without checking whether it is null. So, of course you get a NullReferenceException.

The solution is simply to check before doing that, with “if (go != null)”.

If you want the object to destroy itself when no target can be found, then add an “else” clause that calls Destroy(gameObject).

Handling multiple targets is trickier… you could use FindGameObjectsWithTag (the plural version) to get all the target objects, and then write code that decides what to do about it. For example, you could pursue the closest one, or pursue the one closest to the heading you’re already on.

This will require a little more in-depth coding… if still you’re at the level of struggling with “if” statements, don’t despair, but do consider going through some general programming books/tutorials before tackling that functionality.

        if (go = GameObject.FindGameObjectWithTag ("EnemyShipX")) {
            Target = go.transform;
            mis.LookAt (Target);
        } else if (go != null) {
            Destroy(this.gameObject);
        };

you mean this? that made the game continue if there is no targets buuuuuuuut it doesn’t destroy the gameobject for some xtrange reason, so the projectile prefabs stack up in field … o.o

Well, computers are very literal… you need to be careful to say what you really mean.

Here you’ve said: “if you find a game object, look at it; otherwise, if you’ve found a game object, destroy yourself.” The “if” part and the “else” part are checking the exact same condition, though it may not be obvious because you’re using a rather obscure little language feature inherited from C, which is that you can do assignment as part of an expression and it evaluates to the value being assigned, and then you’re implicitly comparing that to null.

Then in the “else” block, you’re explicitly comparing it to null… but since you’ve already done that, it can never be true.

I’m pretty sure what you meant to say was “if you find a game object, look at it; otherwise, if you didn’t find a game object, destroy yourself.” Didn’t find a game object would mean “go == null”. But even that is being wordier than you need to; how about this version: “if you find a game object, look at it; otherwise, destroy yourself.” The “else” (or “otherwise” in plain English) is enough all by itself. You want one of two things to happen: either it looks at a target, or it dies. Right? So you don’t need two checks — you only need one check.

       if (go = GameObject.FindGameObjectWithTag ("EnemyShipX")) {
           Target = go.transform;
           mis.LookAt (Target);
        } else {
           Destroy(this.gameObject);
        };

This will work. But I’m not a huge fan of combining assignment with expression evaluation like this. It tends to confuse people, especially if they’re new to C-style languages. So I’d suggest instead:

       go = GameObject.FindGameObjectWithTag ("EnemyShipX");
      if (go != null) {
           Target = go.transform;
           mis.LookAt (Target);
        } else if (go != null) {
           Destroy(this.gameObject);
       };

HTH,

  • Joe