Why is my OnCollisonEnter function not working?

Why is this not working? I added this script on a game object without the trigger on.
When I walk against the object, no action is performed.

function OnCollisionEnter(collision : Collision) {
	Debug.Log("Touched");
}

Do you have a rigidbody on at least one of the objects?

I do. It still doesn't work.

2 Answers

2

There is nothing wrong with your script. Adding onto what fafase said, make sure there is a rigidbody attached to at least one of the objects colliding. If this doesn’t work, make sure the objects that are colliding have some form of collider, maybe use a box collider.

Component > Physics > Box collider.

Thanks, worked when I added a rigid body to the game object. Do I need to add a rigid body to every game object I want my player to interact with? I prefer having a rigid body on my player but when I add one and start playing he falls through the ground.

Thanks, worked when I added a rigid body to the game object. Do I need to add a rigid body to every game object I want my player to interact with? I prefer having a rigid body on my player but when I add one and start playing he falls through the ground

as long as one has it (I presume your player) it's fine. Additionally you can use the tagging system in order to determine what your player is allowed to collide with. If your player is falling through the ground, make sure he has a collider himself and when the game is started, make sure the collider is above the ground. Make sure you make a collider for the floor! haha. Let me know if you have any issues, I am here for another 45 minutes :)

You can set isKinematic = true on rigidbodies that are just used for collision detection and not physics simulation.

You do not need to add a rigidbody to every object your player collides with. As long as the player has a rigidbody and the secondary objects have a collider it's fine. This is how I've done it for as long as I can remember. Remember to either tag or attach the script to the relevant object.

You must have some kind of collider propriety on yours objects (characterCollider, BoxCollider, rigidbody, etc)

If you add a collider componente like CharacterCollider, BoxCollider, etc and if you enable option IsTrigger you can implement funcionalities for when you exit or enter on collider object area. For that,you must change function OnColliderEnter for OnTriggerEnter