- Home /
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");
}
Answer by Nercoe · Mar 19, 2013 at 11:14 AM
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.
http://docs.unity3d.com/Documentation/Components/class-Rigidbody.html http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html
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 deter$$anonymous$$e 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. $$anonymous$$ake sure you make a collider for the floor! haha.
Let me know if you have any issues, I am here for another 45 $$anonymous$$utes :)
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.
You can set is$$anonymous$$inematic = true on rigidbodies that are just used for collision detection and not physics simulation.
@Nercoe that's true so long as the secondary object does not move - if it does then collisions can be missed and you will get static collider move penalties to performance.
Answer by Ninita · Mar 19, 2013 at 11:28 AM
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613094706im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I disable 2D camera follow when my player collides with a trigger object? 0 Answers
object not colliding with another object,,.but getting merged and passing each other. 2 Answers
If gameobject on collision with gameobject then destroy 0 Answers
Animation freezes on TriggerStay2D 0 Answers
Can't crash airplane into ground 4 Answers