- Home /
Level selection from main menu?
Hello. My problem is easy, but I don't know scripting very well, I have just now started reading on it.I've followed multiple searches with no results to what I'm looking for (ex: http://answers.unity3d.com/questions/46129/main-menu-creation-multiple-scenes-tutorial) I have my main menu gui, but it's set up to automatically load the "Test Map" after you hit start. I would like to be able to select a level (scene) I would like to play, hit play, and be at that level. My current script limits me only to the Testmap. Does anyone know of a good tutorial, or GUI script, that I can use so that I may select a level I would like to play? Thanks! ~Preston
Answer by Joshua · Mar 23, 2011 at 02:02 AM
If you go to File > Build Settings there is a list of scenes (levels) in your current build (game). Add all your scenes to this list and put them in the right order. The level you want to be loaded on startup should be level zero.
So in this case, put your main menu down as level zero. It will auto load.
Now in your script in the main menu use Application.LoadLevel(x) to love a scene, where x is the number you assigned to the scene in your build settings.
Example:
function OnGui () {
if (GUILayout.Button("Level one")){
Application.LoadLevel(1);
}
if (GUILayout.Button("Level two")){
Application.LoadLevel(2);
}
Edit for tutorial:
Learning GUI and leveloading and information you can retrieve from assigned levels
Answer by mcunha98 1 · Mar 23, 2011 at 02:18 AM
You too load using name of the level, for example:
Application.LoadLevel("LevelLoaderScreen");
Answer by Preston Halo · Mar 23, 2011 at 02:40 AM
This is my script based on what you've said. var newSkin : GUISkin; var mapTexture : Texture2D;
function theMapMenu() { //layout start GUI.BeginGroup(Rect(Screen.width / 2 - 200, 50, 400, 300));
//boxes GUI.Box(Rect(0, 0, 400, 300), ""); GUI.Box(Rect(96, 20, 200, 200), ""); GUI.Box(Rect(96, 222, 200, 20), "TestMap");
//map preview/icon GUI.Label(Rect(100, 20, 198, 198), mapTexture);
//buttons if(GUI.Button(Rect(15, 250, 180, 40), "start level")) { Application.LoadLevel(1); } if(GUI.Button(Rect(205, 250, 180, 40), "go back")) { var script = GetComponent("MainMenuScript"); script.enabled = true; var script2 = GetComponent("MapMenuScript"); script2.enabled = false; }
//layout end GUI.EndGroup();
}
function OnGUI () { //load GUI skin GUI.skin = newSkin;
//execute theMapMenu function
theMapMenu();
} Where can I add the other Application.LoadLevel groups, so that I may have multiple level selectors?
Answer by mubasherikram · Feb 19, 2015 at 11:40 AM
I think ou are facing problems to move across the scenes. For example if you flow is as following
main Menu -> Loading Scene -> respective level
you can store the value of selected scene in player pref and then use the value in loading scene to load the selected level.
For example in loading scene your line of code would be
Application.LoadLevel (PlayerPrefs.GetInt("levelToLoad" ,1 ));