- Home /
How to change a scene from a button
It sounds simple, but ever since Unity 5 rolled in everyone's been giving me false information about
Application.LoadLevel WHICH IS OUTDATED
And now I'm stuck here with a half developed game, frusterated out of my mind thinking about scrapping this entire game because it's entirely based on buttons!
Please.. All I want is some clarification. All the tutorials are outdated. All the forums are too. I just want to change scenes on a button tap. All I require out of the veterans of Unity.
Alright, I've figured this out. Because I'm not very well versed in program$$anonymous$$g, I didn't know that the "Class" had to be the same name as my script. Or something like that. Because as soon as I changed the class name to my script's name.
Thanks for all who responded to this thread and helping this cat out.
Answer by skiedude · Aug 17, 2016 at 08:41 PM
What errors are you getting and your current implementation? From what I found across the interwebs is making sure you add the 'using UnityEngine.SceneManagement;' for Unity 5+. You can call the scene by name in the first example, or just increment your current buildindex if you have your flow sequentially.
Examples pulled from http://forum.unity3d.com/threads/unityengine-application-loadlevel-int-is-obsolete.372915/
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public void GoToSceneThree()
{
SceneManager.LoadScene("scene_three");
}
public void NextScene()
{
Scenemanager.GetActiveScene ().buildIndex + 1;
}
Answer by Mad_Tiger_2409 · Aug 17, 2016 at 08:20 PM
@Pillowfighter9 Look at the documentation: http://docs.unity3d.com/ScriptReference/Application.LoadLevel.html
You can see on the top of the page information that you are looking for: "Use SceneManager.LoadScene"
I hope that I helped :)
Answer by Mukabr · Aug 17, 2016 at 05:49 PM
You have to use https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.html now
Something like this:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class YourClass: MonoBehaviour{
public void OnButtonClick(){
SceneManager.LoadScene("scenename");
}
}
Yeah, it doesn't work. I've tried everything in my own knowledge and the knowledge of the community.
I've attached this script as it's own component and as a trigger with the "Button Press" window in the default button's properties.
No matter what I do, I cannot work this out.
I think I found what might be your issue.
When I got home tonight I spun up 2 quick scenes and tried out what I posted above. I named them 'first' and 'second'. Now I didn't use a button, just a keydown, but it should still work with your button.
using UnityEngine;
using System.Collections;
using UnityEngine.Scene$$anonymous$$anagement;
public class controlme : $$anonymous$$onoBehaviour {
void Update () {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.R))
{
Scene$$anonymous$$anager.LoadScene("second");
}
}
}
I put a red cube in my second scene so I knew I had moved to the next one. Just having that in my code it wasn't working. I checked console and saw:
Scene 'second' (-1) couldn't be loaded because it has not been added to the build settings or the AssetBundle has not been loaded.
To add a scene to the build settings use the menu File->Build Settings...
UnityEngine.Scene$$anonymous$$anagement.Scene$$anonymous$$anager:LoadScene(String)
controlme:Update() (at Assets/controlme.cs:17)
So I opened up my build settings, added my two scenes to the build settings and then closed the build settings window and tried again, voila! worked like a charm. $$anonymous$$ake sure the scene you are moving to and from are added to the scenes that will be built when you choose to do so ( you don't have to build, just add them and close the build window)