- Home /
Changing Scene When Button Tapped
Hi. I created a button by going to create > ui > button. How would I change the scene if the button is clicked or tapped? I know it isn't OnGUI(), right? Please help. Thanks!
if your using unity 5
https://www.youtube.com/watch?v=TEq4P0kDAtE&feature=youtu.be
Answer by podmaster · Dec 16, 2014 at 01:56 AM
You can do:
Application.LoadLevel (Destination);
//FOR EXAMPLE
Application.LoadLevel ("PlayScene2");
Remember, in order for this to work you must open the file > build setting and drag and drop the scenes you are gonna use into the build settings.
REF: http://docs.unity3d.com/ScriptReference/Application.LoadLevel.html
//EDIT// BTW what i do is create this simple script:
//LOAD LEVEL SCRIPT
#pragma strict
var Destination : String;
function LoadScene () {
Application.LoadLevel (Destination);
}
Attach it to the button that loads levels and on the inspecter type in the desire scene destination, then add an event to the button component and use the button it self to execute the LoadScene() function.
Here is another answer about this topic: http://answers.unity3d.com/questions/777818/46-ui-calling-function-on-button-click-via-script.html
//EDIT//
HOW TO DO IT
1.select you button, on the inspector you must have a "button" component if not add it.
2.Then drag and drop the object with the loadlevel script
3.Select the script > function() and when the button gets clicked or taped the selected function will be executed.
http://answers.unity3d.com/storage/temp/37098-untitled-1.jpg
I recommend you to watch this video shows everything you need to know (Almost) https://www.youtube.com/watch?v=EOX6itCuKOc
@podmaster Yes I know about Application.LoadLevel, but how would I detect a click or tap?
Hi, i hope you will see this comment :). I don't want to open another topic for this, so i ask you here. I have the same problem with the last version of unity, how can i do? Application:Loadlevel Needs to be under Void.Update? Please, Help me, i really need it!
Check the documentation http://docs.unity3d.com/ScriptReference/Application.LoadLevel.html
No need to call the Application class inside update. Remember to make this work you have to add the scene to the build settings.
Just call Application.LoadLevel("SCENE_NA$$anonymous$$E") from your class. try it on Start() for a fast test
in the button i already put the button , but in no function i already click but just have a "String Name" the code is same with ur , please help me im urgent
thx i rlly, need this script and setup, keep it up :D
Answer by Stealthygolem · Dec 16, 2014 at 02:38 AM
For your button, there is a place that says "OnClick()" And then some extra stuff here.
If you want to load a scene, when a button is pressed. You first need to have a script up with a function to load a scene, and this script must be attached to an object you refer to. I use my script on the entire "Canvas" parent!
In this script. Create this method, and it has to be public!
public void LoadStage() {
Application.LoadLevel ("YourLevel");
}
In the image I attached, you can see how it looks like for the button! So, whenever this button is clicked (OnClick()), it will see that I reference a script on a gameObject, and a specific method as well! This will exute the method when pressed.
in the button i already put the button , but in no function i already click but just have a "String Name" the code is same with ur , please help me im urgent
Answer by aditya007 · Dec 16, 2014 at 02:56 AM
Do like this (Inside OnGUI() function)
if(GUI.Button (start, "Start Game"))
{
Application.LoadLevel("SceneName");
}
Answer by FFVDGames · Dec 10, 2015 at 05:14 PM
I do this: if (Input.GetMouseButtonUp(0)) { Application.LoadLevel ("Scene name here"); }
Answer by rohitjathot · Jun 22, 2017 at 11:04 AM
//first do this
using UnityEngine.SceneManagement;
//then use this code
SceneManager.LoadScene(index);
//OR
Application.LoadLevel (index);
//index is index of your scene
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
On off game objects 1 Answer
how to run Sprint 1 Answer
New UI Button Dynamic onClick Listener problem 1 Answer
Fading Out UI On Play 1 Answer