Collision Detection

I’m trying to detect what object my character is colliding with.
My simple block of code is always giving me “null” back.

function OnCollisonEnter (theCollision : Collision)
{

	/*if(theCollision.CompareTag == "Floor")
    	{
       		grounded = true;// Sets grounded variable to true.
			//Debug.Log(theCollision.tag); 
    	}
	*/
	
	print(theCollision);
	
}

As you can see the end result would be to detect to tag and use that but it doesn’t even seem to be dectecting what object it collides with. I’m using a rigidbody on cube hitting another cube.
I’m super new but any help would be greatly appreciated.

“theCollision” stores collision data, not the actual object you collided with. As Coreyf716 said you need to use Debug.Log(theCollision.collider.tag); to get the tag of the object you collided with. Also, I think you’re using CompareTag wrong. Try if(theCollision.collider.CompareTag("Floor")). Once again, you were trying to get the tag of the collision data, not the object you collided with. This stuff can be a bit confusing if you’re new. :slight_smile:
I think this should work. Good luck!

private var objectCollided : string;

function OnCollisionEnter(objectCollided : Collision)
{
    //if(objectCollided.gameObject.tag == "BlaBla")
    //{
        //Do Something
    //}
    //print(objectCollided.gameObject.tag);
    //Debug.Log(objectCollided.gameObject.tag);
}

function OnGUI()
{
    GUILayout.Label(Rect(Screen.width/2, Screen.heigth/2,0,0), "Collided With "+objectCollided);
}