- Home /
Reload Level on Collision
Basically, I want to load a level when a collision happens. "Runner" is the player, a FP controller. Where am I going wrong?
var myLevel : String;
function OnCollisionEnter (theCollision : Collision) {
if(theCollision.gameObject.name == "Runner"){
Application.LoadLevel(myLevel);
}
}
Thanks.
Are you sure your Application.LoadLevel is even being called? i.e is the collision happening? Also you don't seem to have set value for myLevel. Until you set a value for this it won't load anything.
Answer by Statement · Mar 27, 2011 at 05:37 PM
If you're dealing with a character controller, then you should use OnControllerColliderHit.
var myLevel : String;
function OnControllerColliderHit (hit : ControllerColliderHit) { if (hit.gameObject.name == "Runner") { Application.LoadLevel(myLevel); } }
Has to be done the other way around...so the Runner hitting the object. Thanks though.
Answer by Statement · Mar 27, 2011 at 05:25 PM
Have you added the scenes you want to be able to load to build settings?
Yes, I have. I think it's becuase it's a character controller.
Answer by Bram 1 · Mar 27, 2011 at 05:34 PM
change Application.LoadLevel(myLevel)
to Application.LoadLevel(0)
don't change anything in the build settings (you can but it's not needed
Answer by Muzz 1 · Mar 27, 2011 at 08:20 PM
var myLevel : String;function OnControllerColliderHit (hit : ControllerColliderHit){ if (hit.gameObject.name == "Runner") { Application.LoadLevel(myLevel); }}