- Home /
Can't get simple script to work?
Hello All,
I'm not TOO bad of a Unity noob, but for some weird reason, I cannot figure this one out... In my scene, I have a Player. On the player, I placed this script:
function Update(){
if(Input.GetKeyDown("escape")){
Application.LoadLevel("MainMenu");
}
Basically, I am trying to load the Main Menu ("MainMenu" scene) when I press the escape key... but for some weird reason, it's not working... I totally realize that it is just some tiny, stupid mistake but I can't seem to figure it out. If anybody could help me out, I would really appreciate it!! Thank you so much!
Have you added $$anonymous$$ain$$anonymous$$enu to the scenes list in the build settings?
Yes I did. I though that was the problem at first, but hen I checked it... Do you have any ideas?
Answer by Fr0stbite · Nov 17, 2013 at 08:56 AM
Maybe:
function Update(){
if(Input.GetKeyDown("escape")){
Application.LoadLevel("1");
}
Answer by clunk47 · Nov 17, 2013 at 02:36 AM
I'd use the keycode instead of a string.
if(Input.GetKeyDown (KeyCode.Escape))
{
//DoYourThing
}
Answer by Tomer-Barkan · Nov 17, 2013 at 08:15 AM
If you're going to use
Input.GetKeyDown()
, then use keycodes and not strings:if (Input.GetKeyDown(KeyCode.Escape)) { Application.LoadLevel("MainMenu"); }
It's better to use
Input.GetButton()
and set the keys in the Input Manager. That makes it easier for you to change if you wish, and it also allows the players to use Unity's key settings to change the buttons as they please. Read how to define the buttons in the input manager here: http://docs.unity3d.com/Documentation/Components/class-InputManager.html. Then useInput.GetButton("NameYouGaveTheKey")
, using the name you defined in the input manager.
But anyway this doesn't really solve yourproblem since your script should work as well... So try the following to debug:
First check whether the Update() is running at all, and add a Debug.Log
right before the if statement right before the if statement. Second, add a debug inside the if statement.
That will help you identfy the problem. If the Update is not running, it might be that the game object or script are disabled, or somewhere else in the update code you are using return;
before the if statement occurs.
If it does go into the if statement, there might be a problem with Application.LoadLevel()
. Maybe the level name is incorrect.
Answer by SrNull · Nov 17, 2013 at 09:06 AM
Have your tried this?:
if(Input.GetKeyDown(KeyCode.Escape)){
Application.LoadLevel("MainMenu");
}
Also make sure your scene is inside the list of the scenes on Build settings window.
Thank you all so much for your help!! I got it. Thank you!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Next Scene Script Help 1 Answer
Reset The Scene ? 2 Answers
How to Set a bool to Individual Scenes (C#) 1 Answer
Gui label text change 0 Answers