How to destroy object after it moves out of screen

Hi i have alot of cars moving in my screen, as time goes by, it will be hundred or thousand cars create randomly moves in my screen. How do i destroy object if it already out from my screen? I want to clean memory for that or it will create thousand not even 1 hour?

To keep things simple, use OnBecameInvisible.

void OnBecameInvisible() {
        Destroy(gameObject);
    }

This will destroy the object when is is out of view of the camera. Here is the API reference: Unity - Scripting API: Renderer.OnBecameInvisible()

Hey there. If you are making a 2D game, you may find a fully explained answer here. Actually I don’t quite agree with Ronnie’s solution, since creating too many colliders and detecting their collisions is very expensive on whatever device you target for. If you are making a 3D game, you may use Camera.WorldToScreenPoint to get the viewport position of the cars based on your indicated camera. You may then check whether the Vector2 position is valid or not. If the x value doesn’t drop in [0, Screen.width] or the y value doesn’t drop in [0, Screen.height], it is obvious that the car is definitely out of the screen. However in order to avoid a jurky destroy you may set the bounds more flexible. Such as [-0.04 * Screen.width, Screen.width * 1.04] or something.

Here is an example:

using UnityEngine;
using System.Collections;

public class CarDetection : MonoBehaviour
{
     private Camera mainCamera;
     public Vector2 widthThresold;
     public Vevtor2 heightThresold;

     void Awake ()
     {
          //Get your mainCamera here. If you are pretty sure that the camera is always the Camera.main you don't need to implement here. Just call for Camera.main later.
     }

     void Update ()
     {
          Vector2 screenPosition = mainCamera.WorldToScreenPoint (transform.position);
          if (screenPosition.x < widthThresold.x || screenPosition.x > widthThresold.y || screenPosition.y < heightThresold.x || screenPosition.y > heightThresold.y)
          Destroy (gameObject);
     }
}

This method doesn’t require any box-colliders and meanwhile the MVP transformation is very efficient. If you are always creating many new cars while destroying old ones, I highly recommend you to have a look at Object Pool to make your program much more efficient.

Please feel free for further questions.

GL & HF!

You should make a trigger box collider, either 2d or 3D depending on your game. Make the collider cover the scene, the cars that are beyond this collider will be destroyed, so place it where you want the cars to be. On a script use OnTriggerExit like so,

void OnTriggerEnter(Collider other) 
    {
        if (other.gameObject.CompareTag ("Car"))
        {
            other.gameObject.SetActive (false);
        }
    }

Modify that to fit your needs.

@thor348k Simple But effective.
Worked For Me
Thank you very much for your answer

This works for 3D too ? I’m trying and can"t make it work. I know it does work for 2D though

I know this is an old post but…

The OnBecameInvisible and OnBecameVisible works but only when there is a “Renderer” attached on the top of the main “GameObject” but if not, you can use:

   Renderer thisRender = GetComponentInChildren<Renderer>();
            if (!thisRender.isVisible)
            {
                Destroy(gameObject);
                Debug.Log("Destroyed by invisibility");
            }

Use GetComponent in childen by the same reason. You shoul to be aware also than the Scene view can affect this process too, so, be sure to close it or minimize it while test those functions. Good luck!!

Here is mine solution

        Vector3 screenCenter = new Vector3(Screen.width / 2, Screen.height / 2, Camera.main.transform.position.z);
        Debug.Log("screenCenter "+screenCenter);

        Vector3 screenHeight = new Vector3(Screen.width / 2, Screen.height, Camera.main.transform.position.z);
        Debug.Log("screenHeight " + screenHeight);

        Vector3 screenWidth = new Vector3(Screen.width, Screen.height/2, Camera.main.transform.position.z);
        Debug.Log("screenWidth " + screenWidth);

        Vector3 goscreen = Camera.main.WorldToScreenPoint(transform.position);
        Debug.Log("GoPos " + goscreen);

        float distX = Vector3.Distance(new Vector3(Screen.width / 2, 0f, 0f), new Vector3(goscreen.x, 0f,0f));
        Debug.Log("distX " + distX);

        float distY = Vector3.Distance(new Vector3(0f, Screen.height / 2, 0f), new Vector3(0f, goscreen.y, 0f));
        Debug.Log("distY " + distY);

        if(distX > Screen.width / 2 || distY > Screen.height / 2)
        {
            Destroy(gameObject);
        }

In some situations, you can consider using a coroutine or invoke methods to destroy instantiated objects after a specific time that they are past the camera.