Unity2D: How to destroy spawned object once it exit out of camera's view?
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 :).
Answer by oStaiko · Aug 23, 2016 at 09:54 PM
The problem with your last one is that you only test for 2 sides, so of course it wont work for 4 sides...
Other than that, I think an easier solution than putting the destroy script on a GameController or such is to put the scripts on the enemies themselves. Just try attaching it to all enemies and make it look like this:
public class DestroyByBoundary : MonoBehaviour
{
public float left;
public float right;
public float top;
public float bottom;
void Update()
{
if (position.x > right || position.x < left || position.y > up || position.y < down)
{
Destroy(this);
}
}
}
Thank you for your reply, Yeah I got this error: The name `position' does not exist in the current context. I change it to transform.postion and destroy (gameObject), becasue destroy(this) did destroy anything. But destroy(gameObject) just deleting/destroying all of my game object before it even gets a chance to leave the spawning point. Thank you for your help. :)
Your answer
Follow this Question
Related Questions
Destroying a spawn object when it exit the main camera view not working 0 Answers
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
Object not visible with skybox 0 Answers
CameraController (object reference not set to an instance of an object) 1 Answer