- Home /
 
Collision.collider returns wrong object
Hi, all,
I just encountered a weird Physics problem, I don't know if this is by design, so I would appreciate it if someone could tell me the definite answer.
Say, I have a hierarchy like this, a root object with two child objects;
 + Root (Kinematic Rigidbody)
 --+ Core  (Sphere collider)
 --+ Platform (Box collider)
 
               and when I drop another cube with rigidbody+collider on the Core, I check the info passed by OnCollisionEnter(Collision),
it was
 Collision.collider : Platform  <-- should be Core
 Collision.gameObject : Root
 Collision.transform : Root
 
               Everything will be right if I put rigidbody on both the Core & Platform.
Is it a bug?
Env: Unity 4.5.2f1 + Win7
Answer by Dhiru · Sep 13, 2014 at 06:27 AM
You can get the collider object by a tag or by name. Then call void OnCollisionEnter(Collision collision) method.
Now the Script should be like this:
 void OnCollisionEnter(Collision collision) {
 if(collision.tag =="otherGameObjectTag"){
 do some stuff here
 }
 if(collision.name =="otherGameObjectName"){
 do some stuff here
 }
 }
 
               Hope this will work
Hi, Thanks for the reply. :)
Huh...Are you sure there're tag and name fields on Collision type? Because they're not on doc, and I checked the source, Collision doesn't have the "tag" or "name" within it.
sorry,I made a mistake. need a small change.
 void OnCollisionEnter(Collision collision) {
 if(collision.collider.tag =="otherGameObjectTag"){
 do some stuff here
 }
 if(collision.collider.name =="otherGameObjectName"){
 do some stuff here
 }
 }
 
                  Now try this
Thanks :) but as I said in question, the problem I encountered is that the "collision.collider" is not the right collider.
In my example, I expected collision.collider.name == "Core", but it turns out to be "Platform";
Your answer