Connect Scenes with a Button

Hey Guys!

Since yesterday i am working on my Home Menu of my Game…
Theres a Button named “Button1”.
I would that when you’re clicking on this Button it’s loading a new Scene…
But… it doesn’t works, here’s the script:

using UnityEngine;
using System.Collections;

public class SwitchScene : MonoBehaviour {
	
	void OnClick ()
	{
		Application.LoadLevel("LevelOne");
	}
}

Can somebody help me how i have to do a button to conect scenes? :stuck_out_tongue:

PS: sorry for my englishxD

Vulzon

that’s not how on click works.
you can either add the object and function manually from the Button inspector, or you can do it programmatically:

void Start(){
this.getComponent<Button>().onClick.AddListener(()=>functionName());
}

void functionName(){
Application.LoadLevel("LevelOne");
}

I assume you are using the new UI and Button1 has a Button-Component.

Add your script to the button. Then add a new EventListener to the button by clicking on the ‘+’ sign in the OnClick()-section of the Button-Component. Drag the GameObject where your script is on (in this case the button itself) into the appearing object field and choose your OnClick()-method from the drop-down menu that appears to the right.

If your method is not on the list you might want to make it public.

Lastly, add your “LevelOne”-scene to the build settings (File > BuildSettings) to make sure your code will work in the built game.

First of all, make sure the spelling and capitalisation of your scene name are exactly the same.

Second, make sure you have added your scene to the current build. (FILE/BuildSettings -add current)

Just an after thought but… you are referring to a UI button right? not a button on the keyboard?