c# problem with enemy and bullet auto destruction

Working on 2d orthographic view type of deal here (space invaders w full screen movement) Trying to get bullet to destroy if reaching certain location or higher (offscreen) cannont get this to work right, here's my code:

using UnityEngine;
using System.Collections;

public class shipMove : MonoBehaviour {

    public GameObject ship;
    Vector3 moveLeft;
    Vector3 moveRight;
    Vector3 moveUp;
    Vector3 moveDown;
    public int speed;

    public Rigidbody bullet;
    public int bulletSpeed;
    public Vector3 bulletCorrection;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

        if(Input.GetKey(KeyCode.LeftArrow)&& (ship.transform.position.x > -220) || Input.GetKey(KeyCode.A) && (ship.transform.position.x > -220))
        {

            transform.Translate(speed * Vector3.left * Time.deltaTime);

        }

        if(Input.GetKey(KeyCode.RightArrow) && (ship.transform.position.x < 220) || Input.GetKey(KeyCode.D) && (ship.transform.position.x < 220))
        {

            transform.Translate(speed * Vector3.right * Time.deltaTime);

        }
        if(Input.GetKey(KeyCode.UpArrow) && (ship.transform.position.z < 65) || Input.GetKey(KeyCode.W) && (ship.transform.position.z < 65))
        {

            transform.Translate(speed * Vector3.forward * Time.deltaTime);

        }

        if(Input.GetKey(KeyCode.DownArrow) && (ship.transform.position.z > -250) || Input.GetKey(KeyCode.X) && (ship.transform.position.z > -250))
        {

            transform.Translate(speed * Vector3.back * Time.deltaTime); 

        }

        if(Input.GetKeyDown(KeyCode.Space))
        {
            Rigidbody clone;

            clone = (Rigidbody)Instantiate(bullet, (ship.transform.position + bulletCorrection), ship.transform.rotation);
            clone.velocity = transform.TransformDirection(Vector3.forward * bulletSpeed);

            if(clone.transform.position.z > 170)
            {
                DestroyObject(clone);
            }

        }

    }
}

Any help would be great, thanks!

1 Answer

1

Well, checking your code just by reading it (without testing in Unity), I can say you have 2 problems here....

First of all, your destruction code is inside your press space to shoot condition. This way, the clone is created, the transform is applied, but the destruction code will only happen IF you're pressing the space button. Try to put it outside your if(Input.GetKeyDown(KeyCode.Space)) code.

Second, and most important of all, your bullet clone is being instanced inside the if(Input.GetKeyDown(KeyCode.Space)) code, which makes the variable local to that. Let's say you press space, the clone object will be created and will move, but next time you press space, another object will be created, and you'll lose reference to the previous bullet, thus it'll never be destroyed, even if you get to the code where you destroy the clone inside the variable.

Resuming, what you have to do to make it work properly, is to make a List of RigidBody (List) declared in the class, not inside the Update method, and everytime you create a new bullet, clone it as you do and create transform for it, and then add it to the list. After all checks are done, place a foreach or for to parse through the list during the Update method, and then, if the bullet is outside your boundary, DestroyObject it and then remove the object itself from your List. This way, you'll never lose reference to a bullet that was created, and you'll destroy it correctly based in your condition.

Thank you for your response. This makes sense to me yet I didn't stress that I'm very much a beginner at this...although I'm sure it's evident. A list is basically an array if I understand correctly (after searching around a bit) that I would put the rigidbodies into, check their locations, destroy when outside of parameters and remove that object from the list I just have no idea how to implement the list Any chance you have a reference link where I could review this. When using the Unity Scripting Reference I am unsure what to search for exactly.

Sorry about that, I saw you created a new post for the list example, and they already provided you with it. I've been away for a while and didn't check any replies so far. Hope you got it working by now, if not, reply here, and I shall provide any help I can for you.