- Home /
How to destroy instantiated objects.
I am making game using c#.
i have an asteroid field that is made by randomly spawning a rock prefab. The player fires using a laser prefab.
i want to make the asteroids destroy the lasers on collision.
i can make the asteroids destroy the player ship as i want however if use a similar code for the lasers it doesn't work.
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "PlayerShip")
{
Destroy(col.gameObject);
}
if (col.gameObject.name == "standardL")
{
Destroy(col.gameObject);
}
}
Is this because both objects are prefabs ?? if so is there away to destroy the lasers when they collide with the asteroids. (this script is attached to the asteroids)
Thanks for any tips in advance.
Answer by Reder13 · Jan 17, 2015 at 08:18 PM
Is this script for the asteroid? if so this is how ive done it in my space shooter:
void OnTriggerEnter (Collider other) {
if (other.tag == "Boundary" || other.tag == "Enemy")
{
return;
}
if (explosion != null)
{
Instantiate(explosion, transform.position, transform.rotation);
}
if (other.tag == "Player")
{
Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
gameController.GameOver();
}
gameController.AddScore(scoreValue);
Destroy (other.gameObject);
Destroy (gameObject);
}
}
pretty much it looks at the incoming thing, if its a boundary or another asteroid/enemy ship it does nothing, if its a player it destroys the player ship, as long as it isnt a boundary or fellow asteroid/ship it gets destroyed and destroys whatever hit it. hope this helps!