Destroying a spawn object when it exit the main camera view not working
Destroying a spawn object when it exit the camera view not working. 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]);
}
}
}
}
I have a 3D cube that spawns enemy prefabs from the bottom and left of the scene near my camera view, so the object enters then when the object exit the camera view, it's suppose to destroy the spawn prefab the script isn't working, it doesn't destroy the spawn objects. I tried it before on this side and it worked, although the public void DestroyAllEnemy was this:
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]);
}
}
}
Follow this Question
Related Questions
Unity2D: How to destroy spawned object once it exit out of camera's view? 1 Answer
(3d) Destroy a GameObject when player walks over it 1 Answer
Spawning Objects from Inspector 1 Answer
I'm trying to spawn an object once another object has been destroyed how do i do that. 2 Answers
HELP WITH TOUCH SCRIPT 0 Answers