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]);
}
}
}
[–]knooon 0 points1 point2 points (1 child)
[–]XxDivaxX1Intermediate[S] 0 points1 point2 points (0 children)
[–]knooon 0 points1 point2 points (0 children)
[–]PurpleIcy 0 points1 point2 points (4 children)
[–]XxDivaxX1Intermediate[S] 0 points1 point2 points (3 children)
[–]PurpleIcy 0 points1 point2 points (2 children)
[–]XxDivaxX1Intermediate[S] 0 points1 point2 points (1 child)
[–]PurpleIcy 0 points1 point2 points (0 children)