- Home /
Question by
AeonIxion · Oct 26, 2012 at 08:51 AM ·
collidertransformoncollisionenter
OnCollisionEnter, collider is the gameobject
Hi, I got a script with an OnCollisionEnter. When I print the name of the collider and the name of the gameobject, it is the same name. I attached the script to a player. In my scene i have a floor and walls. When I move the the cube around, every time it touches a different tile, it triggers the OnCollisionEnter ( as expected), however it says that the player is hit by player.
Here is my code:
void OnCollisionEnter(Collision c)
{
print(collider.name + " " + transform.name);
if(collider.GetComponent<FloorPiece>())
{
var floor = collider.GetComponent<FloorPiece>();
print(floor.color);
renderer.material.color = floor.color;
}
}
Comment
Best Answer
Answer by speedything · Oct 26, 2012 at 09:03 AM
You are assigning the collision as "c", but are then referencing "collider" after that. You should be referencing "c", as at the moment all you're doing is checking the collider of the gameObject that the script is on.
ahh yes yes, you are right. Changed it to c.collider.name and it works. Thanks!