can i count how many objects a Cube is colliding with ?

Hi Guys

My Question is, can i count how many objects a Cube is colliding with ?
Guys can you please suggest me if there is any way to do it?

I was thinking to do it using tag, my idea was to add colliding object to an array than finding its length to know how many objects is colliding with my cube. But after some trial i can only add one Object that is colliding with my cube (but there is 3 objects that is colliding)

Here is the code :

#pragma strict
var newA : Array;
function Start () {

}

function Update () {

}

function OnCollisionEnter(hit : Collision)
{
	print(hit.collider.tag);
	var x : GameObject = hit.collider.gameObject;  // This line only takes one colliding object, How can i take multiple objects. 
	newA = new Array(x);
	print(newA.length);
}

Here is the working code :

var x : int;
function Start () {

}

function Update () {

}

function OnCollisionEnter(hit : Collision)
{
	x++;
	print("x" +x);
}

Just need to put a int variable Inside OnCollisionEnter, and it will automatically increment the variable, the number of time it collide with other object :slight_smile:

I needed to check how many objects were colliding, but I had to check when all of them stopped colliding, so I did a “x–;” in the “Exit” version of the function. I was using triggers in my case. Thanks for the general idea!