- Home /
OnCollisionEnter problem
Basically, I am making a game where the player has to dodge flying cubes... But I can't seem to get one feature to work. I want to "activate" or "add" a rigidbody to the player when one of the flying cubes hit it, but everything that I have tried has not worked... This is the code that people elsewhere have used:
function OnCollisionEnter(hit : Collision) { if(hit.gameObject.tag == "cube") { Debug.Log("Hit"); gameObject.AddComponent(RigidBody); }
}
but this isn't working for me :(
Can anybody help? I don't mind if I'm making a really noobish mistake.
Thanks in advance.
Answer by Joshua · Feb 24, 2011 at 02:45 PM
I think I figured it out (the other two answers are absolutely wrong)
You need to remove the capital B in RigidBody to make it Rigidbody. Rigidbody is the function you want to attached, rigidbody is the variable.
An additional note: from his description, I'm assu$$anonymous$$g his snippet is in a script on the player. Note that OnCollisionEnter() will never fire if the flying cubes do not already have Rigidbody components.
Answer by Xedfire 1 · Mar 05, 2011 at 04:33 PM
Ah, I now know what went wrong. You were all right, so thanks to everyone who posted answers. Basically the problem was a bug (or a complicated feature) in the engine (or my version of the engine)... it only happened when I named my script "collision" (?), and in doing so, I got a strange warning message saying: "This message parameter has to be of type: Collision". It might be just a bug in my downloaded version of Unity (3.2.0), but when I renamed my script to something else, it was fine.
Answer by rimawi · Feb 24, 2011 at 01:44 PM
Hi Try
function OnControllerColliderHit(hit: ControllerColliderHit) { if(hit.gameObject.tag=="cube"){
{ Debug.Log("Hit");
} }
I wouldn't try that. OnControllerColliderHit only works if both the player and the object have a velocity. OnCollisionEnter just always works.
How does that help his request? It doesn't add or activate any sort of rigidbody.
Your answer
Follow this Question
Related Questions
RigidBody 2D x velocity function 1 Answer
Script isn't consistent 0 Answers
,Fast Moving Gameobject Collision and Trigger Check Failed 0 Answers
Don't understand why this on collision script doesn't work. 2 Answers
Need second pair of eyes: rigidbodies with colliders not firing OnCollisionEnter yet again... 1 Answer