- Home /
How to destroy a gameobject's clone when colliding with the ground?
Ok so in my game I have a spawner that spawns clones of a gameobject (Ball). I want the ball's clone to get destroyed when it impacts the ground. I have tried many times to make this work but I have not figured it out yet. Pls help.
Answer by Cornelis-de-Jager · Jul 16, 2017 at 09:18 PM
If you are copying an game object in the scene already it might cause problems. But if you are creating the clones from a Prefab in your asset folder then you can simply add the following code to a script on the clones objects:
void OnCollisionEnter (Collision other) {
Destroy (this.gameObject);
}
If it is a trigger then use:
void OnTriggerEnter (Collider other) {
Destroy (this.gameObject);
}
Ok I dont know how i would make a clone of something that is not in my game already. Sorry i am new at this. Here is my code for my spawner. How would i change it so that the spawner creates a clone of a prefab?
public class SpawnBallOn$$anonymous$$eyPress : $$anonymous$$onoBehaviour {
public GameObject ball;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.Get$$anonymous$$ouseButtonDown (0)) {
Instantiate (ball, transform.position, Quaternion.identity);
}
}
}
Ok i figured out how to make the ball a prefab and add it to my script. I have also done the code that should destroy the object but it won't work. I did a debug to see if the collision was even registered and it apparently wasnt.
Ok i have figured out what to do. Thank you for the help.
Ok i figured out how to make the ball a prefab and add it to my script. I have also done the code that should destroy the object but it won't work. I did a debug to see if the collision was even registered and it apparently wasnt.
$$anonymous$$ake sure of the following:
Your object has a Collider Script.
Ensure that the Trigger Option is Disabled (i.e. not a Trigger) see this link for explination
If you need it as a trigger, however, then use the following code ins$$anonymous$$d:
OnTriggerEnter (Collider other) {
Destroy (this.gameObject);
}
Thank YOu!!! I have been trying to figure this out for hours! Everyone has been saying to attach a script to the player and nothing was working. This, first try, BA$$anonymous$$!
Your answer
Follow this Question
Related Questions
OnCollisionEnter is not working for me!! 1 Answer
Platform Collider Not Working When Enemy Collides With It 1 Answer
OnTriggerEnter2D being called by GameObject without the trigger 1 Answer
Objects falling through EdgeCollider2D on the ground 0 Answers
Player colliding with object then destroying/killing Player 2 Answers