- Home /
How can I make the level restart when someone presses R
Okay, so in my game if you fall out of the world, you continue to fall for ever, I want to make it so you can press R when you fall out to restart something that triggers Application.LoadLeve(1); Ive googled all over the place for this answer could not find it how would I make it detect a keyboard press?
Answer by amphoterik · Jul 16, 2013 at 05:14 PM
You will do something like:
void Update()
{
if(Input.GetKeyDown(KeyCode.R))
Application.LoadLevel(0); //or whatever number your scene is
}
More info on the GetKeyDown method can be found here: http://docs.unity3d.com/Documentation/ScriptReference/Input.GetKeyDown.html
@Wolf12 - Please don't open a new question when comments to existing questions don't get answered fast enough. The above code is C# Create a new C# script and replace the Update() function in that script with the text posted by @amphoterik. Drag and drop it on any object in the scene.
@Wolf12 if this answered your question be sure to mark it as the answer. It helps future readers and gives you karma.
Sorry amphoterik, It did not work, I get a parsing error still
Heres a screenshot of the error http://vvcap.net/db/5fUXhj$$anonymous$$$$anonymous$$T-1w-$$anonymous$$t4nD0i.htp
Answer by zotey · Jul 16, 2013 at 05:20 PM
It is a good idea to give more information in your answer than just a link. This is especially true because the link could eventually break.
Answer by Dimling · Jul 17, 2013 at 06:01 AM
This is how the code should look like. (maybe with a little "R" in Restart to make it work with your script, but should have been with a capital one) ;)
public class Restart : MonoBehaviour{
void Update(){
if(Input.GetKeyDown(KeyCode.R)){
Application.LoadLevel(1);
}
}
}
Maybe you should read some more about basic scripting: http://unity3d.com/learn/tutorials/modules/beginner/scripting
@Dimling thank I'm using this in my game it works very well
Your answer
Follow this Question
Related Questions
Changes to one map affect another. Help?! 1 Answer
Open World In Unity 0 Answers
Massive Open world, is modular a good idea. 1 Answer
How to load levels with colliders? 1 Answer