- Home /
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? Thanks!
Answer by Landern · Apr 24, 2015 at 12:45 PM
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.
Answer by Amoldadu · May 15, 2017 at 07:15 AM
@Dav36id you can also do it without using script... just in a simple way like this
The 1 is the scene number which you want to load... you can see scene numbers in File-> Build setting Thank u
I've been searching for a while, I don't have the LoadOnClick.LoadScene option by default, where do I get this?
I have this problem too! If you've figured it out how to solve it, then can you please tell me?
There is no such option. Unless you make and reference a script which your icon indicates that you have a script referenced.
Answer by $$anonymous$$ · Oct 05, 2017 at 08:07 AM
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()
{
SceneManager.LoadScene("gameplay");
}
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)
{
Application.LoadLevel(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 :)
Honestly I have no idea what you talked about... Well, that's the problem when you're a beginner and english isn't your first language. RIP
There was a slight typo in his answer.
using UnityEngine;
using UnityEngine.Scene$$anonymous$$anagement;
public class SceneLoader : $$anonymous$$onoBehaviour
{
public void LoadScene(int level)
{
Scene$$anonymous$$anager.LoadScene(level);
}
}
Don't forget to name the script in this case SceneLoader.
Your answer
Follow this Question
Related Questions
How do I change textures at a click of a button 2 Answers
Pause Button Menu loader 2 Answers
How do I make a pause menu? 3 Answers
design menu page 4 Answers
Loadlevel and buttons... 2 Answers