# How do I tell if an object is currently colliding with another object?

I can't figure out how to tell if a GameObject is currently colliding with another in Unity. I've looked online and can't find any tutorials or answers that work.

Also, what's the difference between a Collision and a Collider w.r.t. Unity?

My dilemma:

I have a third person controller set up and want it to wall jump. I already have an isGrounded function, I just need to test if I'm hitting anything other than the floor. Otherwise I want it to wall jump backwards.

if (this.OnCollisionEnter){
...
}

if (Collider.OnCollisionEnter){
...
}


But it did not work. I now tried:

function OnCollisionStay(){
Debug.Log("Collision");
}


I checked my logs, console and still can't find anything.

• This is a different question now. I'd suggest choosing an answer to your original question and posting another with more information for the new issue. – chaosTechnician Aug 4 '12 at 3:48

1. The function MonoBehavior.OnCollisionStay() (link) is called every frame for every collider that is touching another collider there are also OnCollisionEnter() (link) and OnCollisionExit() (link) for similar behavior at the start and end of contact. If you want to do something while your object is being collided with, put that function in a script on that object and put your code in there.

2. A Collider is the object that does the colliding. The Collision is the event/data that tells you about the collision. Some explanation can be found here and other places in the Unity docs.

• Could you give an example of this in use? I tried adding if (this.OnCollisionStay()) but that didn't work. – CharlesL Aug 3 '12 at 12:56
• You need to implement a function called OnCollisionStay that contains the code you want executed every frame your object is colliding with something. See the link I provided (here it is again) for an example implementation. – chaosTechnician Aug 3 '12 at 13:55
• Ok, I tried to use OnCollisionStay(){ Debug.Log ("Collision"); }, I played the game, ran into some walls, but still couldn't find anything in my console or logs. Am I still using it the wrong way? – CharlesL Aug 3 '12 at 15:31
• In order to answer that, I'd need to know more about your project setup. Is the function itself set up as a proper function in whatever scripting language you're using? Is it in a script that is on an object that has a collider on it? Is that collider not a trigger? Does it follow the considerations mentioned on the last of the links I provided above (this one)? Etc? – chaosTechnician Aug 3 '12 at 18:38
• The 3rd Person Controller is not (and does not have) a Collider. It's a different type of object. As such, OnCollisionStay won't work. Now that we know this, you probably want the CharacterController.OnControllerColliderHit function which is called when your Controller hits another Collider (link). – chaosTechnician Aug 4 '12 at 2:52

Based on Unity script reference, Collision is a class representing all the information about a collision. It's a parameter passed to Collider.OnCollisionEnter, Collider.OnCollisionStay and Collider.OnCollisionExit events. Collider on the other hand is everything that has a physical representation.

So if you want to know whether an object is colliding with any other object in the game, you have to define a collider for both of them, and implement OnCollisionEnter event for the object you are interested in.

• Could you give an example of this in use? I tried adding if (this.OnCollisionStay()) but that didn't work. – CharlesL Aug 3 '12 at 12:56
• @CharlesL did you even try clicking on any of the links I provided? – Ali1S232 Aug 3 '12 at 14:31
• Ok, I tried to use OnCollisionEnter(){ Debug.Log ("Collision"); }, I played the game, ran into some walls, but still couldn't find anything in my console or logs. – CharlesL Aug 3 '12 at 15:31

Are you sure that at least one of the colliding objects have a non-kinematic rigidbody attached? It did the trick for me.

http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html