OnCollisionStay problem...?

Hi,

I have a problem with a scene. Let’s say I have 2 object in there: Cube and Ground Cube. both of them using the Rigibody.
What I wanted is, when the cube is fall and stay in this Ground Cube it change to other scene.

But when the cube is fall and stay to the Ground cube it usually not change to other scene. I’m using stayCount variable to set the timer before change to other scene.

Any help would be greatly appreciated. And sorry for my bad english :smile:
Thanks

I put this script that i’m using to this cube object.

var stayCount : int = 0;

function OnCollisionStay(myCollision : Collision) {
	
	if (myCollision.gameObject.name == "ground") {
		stayCount+=1;
		if(stayCount==20){
			levelCount+=1;
			Application.LoadLevel("level" +level.levelCount);
		}
	}
}

Hi, welcome to the forum!

The first thing to check is that OnCollisionStay gets called in the first place. You can do this by adding a print statement as the first line of the function just before the “if” statement. If the message gets printed then you know the collision is being detected successfully. Assuming this is OK, move the print inside the first “if” statement and try again. If this is successful then move the print inside the last “if” statement. By doing this, you can establish which of the conditions is failing. For example, if you find that this one is failing:-

if (myCollision.gameObject.name == "ground") {

}

…then check that the object’s name really is “ground” (and make sure the name is all lowercase).

Hi, thanks for your response :slight_smile:
I have checked OnCollisionStay and “if” statement. Both of them is called.

Here’s my complete script for the cube:

var stayCount : int = 0; //variable for set the timer 
static var levelCount : int = 1; //global variable for change level

function OnCollisionStay(myCollision : Collision) {
	Debug.Log("stay");
	if (myCollision.gameObject.name == "ground") {
		stayCount+=1;
		Debug.Log(stayCount);
		if(stayCount==20){
			levelCount+=1;
			Application.LoadLevel("level" +level.levelCount);
		}
	}
}

The problem is when the cube is stay on the ground and timer is running, it usually stop in 19 and 18, but not always. So the scene is not change.
Is it bug? or something wrong with my script?