- Home /
Loading a level on contact
I'm a bit of a noob when it comes to Unity scripting. I'm looking for a script that, when placed on an object, when the player character touches that object, the level re-loads itself and the player starts at the beginning again. I've looked around, but I can't be sure of what is right and wrong. May I have some help please?
Answer by FLASHDENMARK · May 04, 2011 at 03:27 PM
If you are using a character controller :
function OnControllerColliderhit (hit : ControllerColliderHit)
{
if(hit.gameObject.tag == "The tag of the object you are colliding with")
{
Application.LoadLevel("NameOfScene");
}
}
If you are using a rigidbody:
function OnCollisionEnter (hit : Collision) { if(hit.gameObject.tag == "The tag of the object you are colliding with") { Application.LoadLevel("NameOfScene");
}
}
Okay, I tried the first one. I put in the script and ,I hope I got this right, but when you say tg, I think of that thing when you select an object and you can tag it in the corner as " untagged, respawn, finish, and so on. I tagged the object as respawn and the "the tag of the object etc" in the code as respawn and then I put in the name of the scene itself in the second quotes. I put the script in the object, but nothing happens
Answer by Aldwoni_legacy · May 04, 2011 at 03:18 PM
Application.LoadLevel("nameofscene");
I just tried that script, but the level just endlessly reloads itself right at the start with no contact needed.
Your answer
Follow this Question
Related Questions
Load Level when Key is pressed 2 Answers
level loader script wont work 1 Answer
Load a scene/level when enemy is close... 1 Answer
how do you actually stop a game? 3 Answers
Loading a level. (Few questions) 1 Answer