x


Scene not loading when pressing menu button

I have created a simple menu using a GUISkin. I added in the code to load a new scene when a menu button is pressed, but when I try it, nothing happens. Is there anything I missed in the process of creating a working menu?

Example code:

if(GUILayout.Button ("Credits")){
	yield new WaitForSeconds(0.35);
	Application.LoadLevel("Credits");
}
more ▼

asked Nov 20 '09 at 11:47 AM

ericksson gravatar image

ericksson
1.9k 11 14 45

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

When you test your menu from Unity and try to press the button you will probably notice an error message: Level Credits couldn't be loaded because it is not added to the build settings. The code is fine and the solution straightforward. You just have to go into the Build Settings... option in the File menu. You will notice that the Credits scene you are trying to load is not there. You just have to drag it from your Project Panel in the Scenes to build section of the Build Settings window. Your menu should work now.

Also it is very important that your Main menu scene (or the Scene that you want your game to load when you start it) be the first one in the Build Settings. The order of the other scenes in the Build Settings is not important.

more ▼

answered Nov 20 '09 at 11:47 AM

ericksson gravatar image

ericksson
1.9k 11 14 45

There is one case when the order is important, and that is if you write code which relies on the level's index to see which level is loaded. For example, see the documentation for MonoBehaviour.OnLevelWasLoaded.

Such logic is worth avoiding, but if you have any, then be warned that the scene order in the Build Settings will matter.

Nov 20 '09 at 08:04 PM Bampf
(comments are locked)
10|3000 characters needed characters left

hi i haveing difrent problem then i use application.loadlevel in my script then i run my project i load main meniu scean and then it's automaticly loading next lvl but i made GUI button to do that my script looks like this were i added application.loadlevel

private void DisplayButton() { if(GUI.Button(new Rect(Screen.width/ 2 - 50,STAT_STARTING_POINT + (10 * LINE_HEIGHT),STAT_LABLE_WIDTH,LINEHEIGHT),"Create")); { GameObject gs = GameObject.Find(" _gamesettings"); GameSettings gsScript = gs.GetComponent(); gsScript.SaveCharacterData(); Application.LoadLevel(1); } }

more ▼

answered Jan 23 '12 at 11:54 PM

edvinaszz gravatar image

edvinaszz
1

(comments are locked)
10|3000 characters needed characters left

first of all the order of scenes is important when you want to load them with their index number and don't want to use their name. also if the problem would be about the build settings list, an error message would be shown in your game telling that you should add the scene to your list. i think you have another problem if you don't see any error(you did not say anything about it). try to print something above your Application.LoadLevel () function to see if the click even is triggered or not.

more ▼

answered Nov 20 '09 at 08:13 PM

Ashkan_gc gravatar image

Ashkan_gc
9.3k 33 56 120

Thank you for this great comments. You are right about the ordering of the scenes being important in certain cases. I never loaded a scene by it's index, so I didn't know about that. And, I was receiving the error: Level Credits couldn't be loaded because it is not added to the build settings, which is easily solved by adding the scene in the Build Settings. I think it's a common mistake among beginners.

Nov 21 '09 at 06:57 AM ericksson
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5273
x3815

asked: Nov 20 '09 at 11:47 AM

Seen: 5778 times

Last Updated: Jan 23 '12 at 11:54 PM