I am trying to make a simple main menu script that quits the game when escape is pressed, loads scene 1 when F1 is pressed, and loads scene 2 when F2 is pressed. I am not getting any compiler errors and when the project is built and the “escape to quit” aspect of the main menu script works fine but neither scene 1 or scene 2 load when F1 or F2 is pressed. The main menu scene and both of the other scenes are in the build settings and both of the other scenes are labeled 1 and 2. If there is nothing wrong with my script and they’re in the build setting properly what could it be?
I guess probably you have not added the scene in the Build Settings.
Anyway if still that doesn’t work try the following code:
if(Input.GetKey(KeyCode.Escape))
{
Application.Quit();
}
if(Input.GetKeyDown(KeyCode.F1))
{
Application.Loadlevel("Write your 1stscene name here");
}
if(Input.GetKeyDown(KeyCode.F2)){
Application.Loadlevel("Write your 1stscene name here");
}
What GameObject is this script attached to? Scripts, on their own, do nothing. They must be attached to a GameObject that exists in the scene and is currently active.
Do you know that the Update code is being called? Do you know if the Input commands are being recognized?
Try changing the script to add some debug lines and watching the console output:
Update(){
// this line should show up in the console a lot
Debug.Log("inside update function");
if(Input.GetKey(KeyCode.Escape)){
// this line should show up in the console anytime escape is down
Debug.Log("escape is down");
Application.Quit();
}
if(Input.GetKeyDown(KeyCode.F1)){
// this line should show up once each time F1 is pressed
Debug.Log("F1 was pressed");
Application.Loadlevel(1);
}
if(Input.GetKeyDown(KeyCode.F2)){
// this line should show up once each time F2 is pressed
Debug.Log("F2 was pressed");
Application.Loadlevel(2);
}
}
You should pass a string inside Application.Loadlevel()'s parameter. ex : Application.Loadlevel(“1”);
Update(){
// this line should show up in the console a lot
Debug.Log(“inside update function”);
if(Input.GetKey(KeyCode.Escape)){
// this line should show up in the console anytime escape is down
Debug.Log(“escape is down”);
Application.Quit();
}
if(Input.GetKeyDown(KeyCode.F1)){
// this line should show up once each time F1 is pressed
Debug.Log("F1 was pressed");
Application.Loadlevel("1");
}
if(Input.GetKeyDown(KeyCode.F2)){
// this line should show up once each time F2 is pressed
Debug.Log("F2 was pressed");
Application.Loadlevel("2");
}