0
\$\begingroup\$

I'm trying to destroy spawned objects once it exit out of camera view automatically/straight away. You see I'm trying to make a flappy brids type of game but things also comes in from the bottom and if they get a really high score then things will be spawned in from above and from the left side. So, so far this is what my game looks like (I'm still working on it). However I'm having problems with deleting spawned objects once it exit out of camera's view, I tried all sorts of things like:

private var hasAppeared : boolean; 

function Start ()
 {  
hasAppeared = false;
}

function Update() 
{

if (GetComponent.<Renderer>().isVisible)
{
    hasAppeared = true; 
}

if (hasAppeared)
{
    if (!GetComponent.<Renderer>().isVisible)
        {
            Destroy(gameObject);
        }
    }               

}

But with this script, it doesn't destroy the game object quickly enough that and only some of them is being deleted. I also tried this method:

     public void OnBecameInvisible() {
        Destroy(gameObject);
      }

But not all of them is being deleted as well and it also takes a long time for any of my game objects to be destroyed. My last result was this script, it destroys the game object straight away once it exit out of the camera's view (which is what I want) but only these sides works where as the other's don't, I think it destroys my enemies (spawned objects) before it has even entered the camera's view. How do I get the script to work on this side? This is my script:

void Update ()
{
    DestroyAllEnemy ();
}

public void DestroyAllEnemy()
{
    GameObject[] enemies = GameObject.FindGameObjectsWithTag ("enemy");
    if ((enemies.Length > 0) && (camera != null)) {
        for (int i = (enemies.Length - 1); i >= 0; i--) {
            // just a precaution
            if ((enemies [i] != null) && ((camera.WorldToViewportPoint (enemies [i].transform.position).x > 1.03f) || 
                (enemies [i].transform.position.y < -6f))) 
            {
                Destroy (enemies [i]);
            }
        }
    }
}

Thank you :).

\$\endgroup\$
2
\$\begingroup\$

OnBecameVisible and its counterpart has a few gotchas.

  1. It works only if the gameobject has geometry.
  2. The editor view also triggers these events.

You might want to look at the CullingGroup API if you want to avoid these issues.

\$\endgroup\$
0
\$\begingroup\$

Don't allow removal until the object has been seen at least once.

bool readyToRemove = false;

void OnBecameVisible() {
    readyToRemove = true;
}

Then use one of your other methods to remove, for example:

public void OnBecameInvisible() {
    if(readyToRemove)
        Destroy(gameObject);
}

As an aside, I'd suggest you look into recycling the objects with object pooling.

\$\endgroup\$
  • \$\begingroup\$ Thank you for your reply, I can see in the scene window that my objects aren't being destroyed but when I pause my game and scroll toward the spawn objects they're deleted? am I right? does OnBecameInvisible do that, I guess I need to learn more about the subject. \$\endgroup\$ – Jessca Stone Aug 23 '16 at 20:42
  • \$\begingroup\$ Those methods are triggered by any camera, including the scene camera. \$\endgroup\$ – MichaelHouse Aug 24 '16 at 6:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.