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][1] (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][2] 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 _!= null) && ((camera.WorldToViewportPoint (enemies *.transform.position).x > 1.03f) ||*_
_ (enemies .transform.position.y < -6f)))
* {
Destroy (enemies );
}
}
}
}
Thank you :).
[1]: http://imgur.com/fm5NMKL*_
_*[2]: http://imgur.com/hY3bcfi*_