Button OnClick load scene

Hi! Im trying to create a simple menu. Im adding a Button but I dont know how to OnClick load game scene (I call it Main).

How I can do it?


Go and watch the UI.Button tutorial. After you watched and created your script that will eventually be attached the OnClick event you will want to put in your event one of two Application methods.

Application.LoadLevel(String) which will load a scene by it’s name in the build settings or

Application.LoadLevel(int) which will load the scene by the index value in the build settings.

@Dav36id you can also do it without using script…
just in a simple way like this94118-snip.png

The 1 is the scene number which you want to load…
you can see scene numbers in File-> Build setting
Thank u

I’m not sure if you can do it without a script… In Amoldadu’s screenshot, there appears to be a script attached…

Anyway, loading a scene on a button click isn’t that hard at all.
There are two ways you can load scenes - by name, or by number.

In Javascript (by name):

#pragma strict
import UnityEngine.SceneManagement;
function LoadLevel()

And in C# (by number) - remember to call this ‘SceneLoader.cs’:

using UnityEngine;
using System.Collections;

public class SceneLoader: Monobehaviour {

     public void LoadScene(int level)

Make sure that all your scenes are chronologically ordered in your build settings.
Create an empty gameObject and drop your new code in.
Drag this gameObject into the placeholder in your button inspector.
You should find a ‘LoadLevel()’ function (or something similar) in the dropdown menu.

I’d be very surprised if there was a scriptless scene loader button…

Hope I helped :slight_smile:

There was a slight typo in his answer.

using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneLoader : MonoBehaviour

    public void LoadScene(int level)