Not working code?

it was supposed to open Level1 when the start button was pressed but it says :

Level ‘Level1’ (-1) couldn’t be loaded because it has not been added to the build settings.
To add a level to the build settings use the menu File->Build Settings…
UnityEngine.Application:LoadLevel(String)
Mainmenu:OnGUI() (at Assets/Scripts/Mainmenu.cs:22)

my code:

using UnityEngine;
using System.Collections;

public class Mainmenu : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void OnGUI(){
		//menu box
		GUI.Box(new Rect(10,10,100,200), "Menu");
		//start buttton
		if (GUI.Button (new Rect (30,30,50,20), "Start")) {
			Debug.Log ("Loading....");
			Application.LoadLevel("Level1");
		}

	}

}

2 Answers

2

To add a level to the build settings use the menu File->Build Settings

It tells you how to fix it…You can’t load a scene that’s not added to the build settings window.

thanks both of guys you really helped me.

it might be easier to use (less ambiguity)

Application.LoadLevel(x);//where x is the index in build settings.

Have you added it to build settings?
I hope this helps.

Maurice