- Home /
what object hit me? easy rigidbody question
trying to print the name of an object that collides with a rigid body. There's a lack of clear simple examples of this in the scripting documentation. There's something wrong with the code below but I can't tell what..
I get error messege "An instance of type 'UnityEngine.Collision' is required to access non static member 'gameObject'." Being a newbie i don't understand the error message. Can you hep.
function OnCollisionEnter(collision : Collision) {
var whohitme = Collision.gameObject ;
print ("i was hit by"+whohitme);
}
Here's another version I'm trying but this also does not work. This is a simple one I'm sure.
function OnCollisionEnter(collision : Collision) { var whohitme =Collision.collider ; print ("i was hit by"+whohitme);
}
Answer by Mike 3 · Jun 03, 2010 at 07:56 PM
changed your code to work
you were using the class name instead of the variable name to get the gameobject
also , swapped it to print out the name of the gameobject, not the gameobject itself
function OnCollisionEnter(collision : Collision)
{
var whohitme = collision.gameObject;
print ("i was hit by"+whohitme.name);
}
Ah thank you so much! What a stupid mistake! All the unity script reference gave was:
"var collider : Collider Description The Collider we hit (Read Only)."
Which was not very helpful at all. The script documentation should contain very simple examples showing how to access each class variable like the one above that would be perfect!!