How to write collision event code?

Hi. I am taking a tutorial on basic collisions on youtube, and i dont know much about unity. I tried the OnCollisionEnter function. Monodevelop wouldnt even recognize it as a function. I even copied the exact code. What di I do? Here is the Code:

function OnCollisionEnter(){
    Debug.Log("Hit Something!");
}

scrip

This code looks fine. Are you sure you pasted it into a Javascrip file and not a C# file? Note that both objects must have a collider, and one of the two objects must have a rigidbody for this function to trigger.

You might have to pass it the Collision class for it to work. So: void OnCollisionEnter(Collision col){} Or in your case (Javascript): function OnCollisionEnter(col : Collision){}

Put one into the first person controller and it still wont work.

as in a rigidbody. But i didn't enable gravity because then the object freaks out.

What colliders do you have on the object? If you have more than just one collider (player, rigidbody, physics) that will cause issues

1 Answer

1

in unity window go to Edit->preference->
->external tool->external script editor
select mono develop again if u have already selected.
then u will have all functions and keys will show effectively