- Home /
Question by
xdarkasgaming · Jun 10, 2020 at 07:33 PM ·
cameragameobjectunity 2ddestroygameobject
How can I destroy gameobject when passing the screen
Hello, Can anyone help me about this code. I can't destroy the gameObject when getting out of the camera screen. I hope can someone figure this out. I'm newbie in programming trying to build my first game app.
public float speed = 10.0f; private Rigidbody2D rb; private Vector2 screenBounds;
// Use this for initialization
void Start()
{
rb = this.GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(-speed, 0);
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.x));
}
// Update is called once per frame
void Update()
{
if (transform.position.x < screenBounds.y * 2)
{
void OnBecameInvisible()
{
Destroy(gameObject);
}
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613000621im_/https://answers.unity.com/themes/thub/images/avi.jpg)