- Home /
Another Collision Question
I've been through numerous related answers on here but just can't get my collisions working properly.
I've have a prefab gameobject called Bullet with the following script attached. The bullets are physically colliding with another prefab game object which has the tag "asteroid". However I am now adding a scoring system and need the score to change when the two objects collide. Both have a rigid body and a sphere collider attached. Neither is isTrigger or kinematic. But I can't get the debug to work when they collide.
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Asteroid")
{
Debug.Log("hit");
}
}
Hm, strange. Do this and see what's printed to the console log:
void OnCollisionEnter(Collision collision)
{
Debug.Log(collision.gameObject.tag);
}
Answer by MrSteve1 · Dec 11, 2013 at 04:47 PM
I would check that the Tag you assigned the object is exactly the same including capitalization,I cant help noticing in your question it is not, if not that can you post a pic of your setup with the onbjects Selected?
Answer by EX_Darius · Dec 11, 2013 at 04:49 PM
Make sure the tag you are referring to is spelled exactly the same in your script.
You can also try this if ^ doesnt work try this: (make sure to enable the isTrigger option for either of the objects)
void OnTriggerEnter(collider other)
{
if (other.gameObject.name == [the object name here] )
{
Debug.Log("Object has been hit!")
}
}
There is no need to set either of the object's rigidbodies to "IsTrigger". In fact, doing so will make the objects pass through each other.
I am guessing my answer wasn't there when you began typing yours.
Yup yours wasnt there when I started writing this. However for the function "OnTriggerEnter" you do need to have 1 "isTrigger" enabled.
Answer by harveyhb · Dec 12, 2013 at 11:17 AM
Thanks for your help, both the bullet and the asteroids were prefabs and I hadn't used the tag on the prefab, just on the objects in hierarchy.
Working now!
Your answer
Follow this Question
Related Questions
Collision with no contact? 2 Answers
Unparent object on collision? 3 Answers
Transform collider not detecting collision on rigidbody collider 2 Answers
onCollisionEnter function called from another object. 1 Answer
Change Direction on Collision 6 Answers