Nirmal Purohit
Nirmal Purohit

Reputation: 67

Detecting a collision between two colliders

I'm developing a game in unity using UnityScript. I've initially created two objects, a sphere and a cube, having respective colliders. Now I'm trying to detect a collision between them using the below UnityScript function. But I'm not able to detect collision. I've also added a rigid body component to both of them. How can I detect a collision?

function OnCollisionEnter(col : Collision)
{
    if(col.collider.tag=="Cube")
    {   
        Debug.Log("collision");
    }

Upvotes: 0

Views: 7903

Answers (1)

Acorn
Acorn

Reputation: 867

Things to verify:

  1. Make sure the script(s) are attached to the game objects
  2. UPDATED: Make sure the class you have this code in inherits from MonoBehaviour (C#, boo)
  3. Make sure collider components are attached to the game objects and that "Is Trigger" isn't checked as OnCollisionEnter won't be fired if it is.
  4. Make sure the gameobject has the "Cube" tag assigned in the inspector

Also, the rigidbody component only adds physics to the objects and is not necessary when simply detecting collisions.

Upvotes: 2

Related Questions