- Home /
The question is answered, right answer was accepted
OnCollisionEnter not working
As usual, OnCollisionEnter stops working at the tiniest little piece of dust that floats by. Well, here's my code, very simple and SHOULD work:
function OnCollisionEnter(collider:Collision)
{
if (collider.gameObject.tag == "ground")
{
Debug.Log("colliding with ground");
}
if (collider.gameObject.tag == "layerPad")
{
Debug.Log("colliding with layer pad");
}
}
Alright, layerPad has a rigidbody attached to it plus a box collider. The player has also has a box collider attached to it (no rigidbody (and no, I am not using any transform stuff)). Now when these 2 objects collide, they stop moving since they hit, but no debug.log. They have rigidbodies, they've got box colliders (both of the same type, they are both 3D colliders) the rigidbody is not a 2D one, I'm not using transforms, why the heck doesn't it debug.log? Should't this be working? (they are both marked as not to be a trigger). I've been stuck on this for THREE HOURS. It's driving me nuts. Anyone have any ideas what I am doing wrong?
All you have proved is that you are not getting into the body of your ifs.
You are assu$$anonymous$$g that its because you aren't getting the callback but you have no evidence yet to support that assumption.
Assumptions are deadly and the antithesis of debugging.
The first thing you should do is put a debug at the top of your callback and BEFORE the ifs to see if it gets called.
Then come back and tell us the result and we'll elp you froim there.
are the objects children of another object? if yes, is the script in the children itself? if no you should access the children.
On another subject, have you tried only "collider.tag" ins$$anonymous$$d of "collider.gameObject.tag"?
Just to be sure, have you attached this script to your Player game object?
The objects are physically colliding, you said.
This leaves the problem to be that the tags are incorrect on the objects that contain the scripts and rigidbodies.
Yep, I have attached the scripts to the desired game objects. And yes, both of the colliders are children of other game objects. Could this be the problem? Also, I've already triple checked tags but I'll try the Debug.Log outside of the if statement as you said. @Jeff sorry, I'll remember that for next time.
Answer by mightybob · Jun 20, 2014 at 07:45 PM
It's fixed now. For some reason Unity wouldn't collide with children objects. Big thanks to @Hikaros for pointing it out! Here's my finished code for anyone else with the same problem:
function OnCollisionEnter(collision:Collision)
{
if (collision.gameObject.rigidbody.position.x > rigidbody.position.x)
{
Debug.Log("right collide");
}
else
{
Debug.Log("left collide");
}
if (collision.gameObject.rigidbody.position.y > rigidbody.position.y)
{
Debug.Log("top collide");
}
else
{
Debug.Log("bottom collide");
if (collision.gameObject.tag == "layerPad")
{
ChangeTo(collision.gameObject.GetComponent(BounceScript).targetLayer, collision.gameObject.GetComponent(BounceScript).targetHeight);
}
}
}
Follow this Question
Related Questions
Unity Collison Problem 1 Answer
C# GameObject is not detecting collision with Character Controller 2 Answers
C# Collision Detection Help 0 Answers