- Home /
Other
OnCollisionEnter is not working for me!!
Hello there I'm trying to use OnCollisionEnter to destroy an object (just like collecting a coin), and I have 2objects and both of them have rigid body and colliders I have added my script to one of them and the name that I wrote is the name of the other object which doesn't have the script. I'm trying to destroy this gameObject whenever it was hit by the other gameObject's collider . But the script doesn't work. Please help!!!! Thanks!!
public class DestroyCubes : MonoBehaviour
{
void OnCollisionEnter (Collision col)
{
if(col.gameObject.name == "Cube")
{
Destroy(col.gameObject);
}
}
}
Answer by FlaSh-G · Aug 12, 2017 at 01:27 PM
Put a few Debug.Logs in there to find out what exactly is not working.
public class DestroyCubes : MonoBehaviour
{
void OnCollisionEnter (Collision col)
{
Debug.Log("OnCollisionEnter works.");
if(col.gameObject.name == "Cube")
{
Debug.Log("The GameObject name is Cube.");
Destroy(col.gameObject);
}
}
}
Then you can continue to investigate.
I don't know what was I missing but I copied your code and it worked! :D Anyway thank you for your help sir.
Follow this Question
Related Questions
How to set collision for an object with specific collider size? 3 Answers
Collision detection problem 0 Answers
Weird ContactPoint2D on two BoxCollider2D collision 0 Answers
OnTriggerEnter2D working, OnColliderEnter2D not working 1 Answer
How do I detect which collider is being collided with? 0 Answers