Destroying instantiated game objects from prefab when coliding with the ground
Hi, im working on a game where you need to dodge asteroids that fall from the sky. I made a random spawner script with empty game object. from a youtube tutorial. Now I dont know how to destroy asteroids when they collide with the ground. Im new at making games. I tried many diferent options.Puting destroyer scripts i found online in the spawner script, didnt work. making a new script and adding it to my asteroid prefab, didnt work. I dont know what to do. im using this script as a destroying script :
using UnityEngine;
using System.Collections;
public class Destroyer : MonoBehaviour
{
void OnCollisionEnter(Collision other)
{
Destroy(this.gameObject);
}
}
If anyone can help me i would be very grateful!
Answer by Empowerless · Jul 14, 2020 at 05:25 PM
Assuming this script is attached to the asteroid prefab it should work I think.
Does the asteroid prefab and the ground have collider components? (such as sphere collider or box collider?)
Script is attached to the asteroid prefab, both have colliders, and asteroid has rigidbody 2d. When asteroid hits the ground it just stays there, rolling around. $$anonymous$$y player can push them, but they don't get destroyed
Does any of the colliders have 'IsTrigger' boxed ticked? They shouldn't have if you want to use OnCollision functions.
Alternatively you can use OnTriggerEnter function if you tick the box.
https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
They dont have isTrigger ticked, if i tick that box they fall thru the ground.