Destroy Gameobject when he's out of the Area

Hi guys!
Sorry for this question but, i’m already seeing a lot of tutorials and none as useful yet. So… I have my gaming area set in:

xMin = -10
xMax = 10
yMin = -4.3
yMax = 4.3

And i have my collider code:

    public GameObject explosion;        // Prefab of explosion effect.

 
    void Start ()
    {

    }

    void OnExplode()
    {
        // Create a quaternion with a random rotation in the z-axis.
        Quaternion randomRotation = Quaternion.Euler(0f, 0f, Random.Range(0f, 360f));
     
        // Instantiate the explosion where the rocket is with the random rotation.
        Instantiate(explosion, transform.position, randomRotation);
    }
 
    void OnTriggerEnter2D (Collider2D col)
    {
        // If it hits an enemy...
        if(col.tag == "Inimigo")
        {
            // ... find the Enemy script and call the Hurt function.
            //col.gameObject.GetComponent<Inimigo>().Hurt();
         
            // Call the explosion instantiation.
            OnExplode();
         
            // Destroy the rocket.
            Destroy (gameObject);
        }
        // Otherwise if the player manages to shoot himself...
        else if(col.gameObject.tag != "Player")
        {
            // Instantiate the explosion and destroy the rocket.
            OnExplode();
            Destroy (gameObject);
        }

    }

I want to know if there is a way to know when my object left the gaming area he can be destroyed. I try to pick up his position and compare with a vector3 but doesn’t work

This is the code where i try to compare the x Axis from a variable gArea (Gaming Area) and position of my object instance.

    void Update ()
    {
        gArea = new Vector3 (Mathf.Clamp(rocket.position.x, -10f, 10f), Mathf.Clamp(rocket.position.y, -4.3f, 4.3f), 0.0f);
        // If the fire button is pressed...
        if(Input.GetButtonDown("Fire1"))
        {

                rocket.transform.localScale = new Vector3(xScale, yScale, 0.0F);
                // ... instantiate the rocket facing right and set it's velocity to the right.
                Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
                bulletInstance.velocity = new Vector2(speed, 0);

            //Compare x position to destroy the object
            if (bulletInstance.position.x == gArea.x)
            {
                Destroy(rocket);
            }
        }


    }

Somebody can help me ?

Assuming you are using a collider of sorts (BoxCollider2d, CircleCollider2d, PollygonCollider2d), you can use OnTriggerExit2D

void OnTriggerExit2D(Collider2D other){
    Destroy(rocket);
}
1 Like

In your code you are changing ‘bulletInstance’ every time you fire a bullet, you are then performing your check to see if it should be destroyed right inside your fire button input condition. So it will only check if it should destroy the object as you fire a new object, which replaces the first object since that part of your code is above the destroy check. It depends on exactly the effect you want, but I would suggest that you take your destroy check and place it in its own script called something like ‘DestroyOutOfBounds’ and attach that script to the prefab of your bullet. You will just then need to feed it the correct information in terms of your play area. Based on the info you posted my guess is something like this would work;

float xMin = -10f;
float xMax = 10f;
float yMin = -4.3f;
float yMax = 4.3f;

void Update()
{
    if (transform.position.x > xMax || transform.position.x < xMin)
    {
        Destroy(this);
    }
}
1 Like

Thx guys!

I got it!:slight_smile: