Reputation: 345
Im making a 2D Game in Unity 2D(4.3), and I need to destroy the prefabs that get instantiated when those prefabs go off the screen. I have written some code to spawn the Objects, but then I want to delete those prefabs when they go off screen. Here is the code I have written so far.
To Generate prefab (C#):
void Update () {
float y = Random.Range(-4.53f, 2.207f);
if(x < 2000) {
Instantiate(obstacle, new Vector3(y, x * 6.0f, 0),Quaternion.identity);
x++;
}
//Debug.Log(x);
}
To destroy the prefab(C#):
/*************************************************************************************************
* GET INSTANTIATED OBSTACLE
* AND DESTROY IT ON EXIT
* TO SAVE MEMORY
**************************************************************************************************/
GameObject clone = (GameObject)Instantiate (obstacle);
/*if(clone.transform.position.y == -11)
{
Destroy(clone);
Debug.Log("Destroy");
}*/
Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
if (screenPosition.y > Screen.height || screenPosition.y < 0)
{
Destroy(gameObject);
Debug.Log("Destroy");
}
However, the code to destroy the object is not working, but is not getting an error either. It does output "Destroy" after the prefabs go off screen so I know its something wrong with the code to destroy them.
Thanks
Upvotes: 1
Views: 21015
Reputation: 21
You can use the following function to detect when the object is out of screen, and then destroy it or anything else according to your game logic.
public bool IsOutOfScreen(GameObject o, Camera cam = null)
{
bool result = false;
Renderer ren = o.GetComponent<Renderer>();
if(ren){
if (cam == null) cam = Camera.main;
Vector2 sdim = SpriteScreenSize(o,cam);
Vector2 pos = cam.WorldToScreenPoint(o.transform.position);
Vector2 min = pos - sdim;
Vector2 max = pos + sdim;
if( min.x > Screen.width || max.x < 0f ||
min.y > Screen.height || max.y < 0f) {
result = true;
}
}
else{
//TODO: throw exception or something
}
return result;
}
public Vector2 SpriteScreenSize(GameObject o, Camera cam = null)
{
if (cam == null) cam = Camera.main;
Vector2 sdim = new Vector2();
Renderer ren = o.GetComponent<Renderer>() as Renderer;
if (ren)
{
sdim = cam.WorldToScreenPoint(ren.bounds.max) -
cam.WorldToScreenPoint(ren.bounds.min);
}
return sdim;
}
Upvotes: 0
Reputation: 563
You can make 4 quads on the 4 sides of screens and with them you attach boxCollider and check its isTrigger. After that add the below script to each quad that checks that if something collides with it in its OnTriggerEnter and in there you may check the tag of the instantiated object or you may destroy each object that collides with it(depends on the game). Use the code below
//for 3d games
void OnTriggerEnter(Collider other) {
//you may check the tag of the 'other' object here to make sure if its your instantiated object
//if(other.gameObject.tag=="yourInstantiatedObjectTag")
Destroy(other.gameObject);//dont forget to check the isTrigger of the quad or else the event will not trigger
}
//for 2d games
void OnTriggerEnter2D(Collider other) {
//you may check the tag of the 'other' object here to make sure if its your instantiated object
//if(other.gameObject.tag=="yourInstantiatedObjectTag")
Destroy(other.gameObject);//dont forget to check the isTrigger of the quad or else the event will not trigger
}
Upvotes: 1
Reputation: 765
You can make a component that will destroy self when position is out of camera, then attach this component to the obstacle.
void Update() {
float y = Random.Range(-4.53f, 2.207f);
if(x < 2000) {
GameObject clone = (GameObject)Instantiate(obstacle, new Vector3(y, x * 6.0f, 0),Quaternion.identity);
clone.AddComponent(typeof(DestroyMySelf));
x++;
}
}
And this component attach to the obstacle will destroy self.
public class DestroyMySelf : MonoBehaviour {
void Update() {
Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
if (screenPosition.y > Screen.height || screenPosition.y < 0)
Destroy(this.gameObject);
}
}
Upvotes: 8