- Home /
How to get button to switch scenes
Hello i keep trying to get my button to switch scenes when i click it but nothing happens when i Build and run the game. I put all my scenes in the Build Settings and they are in order but it does nothing if someone could help me thatd be really awesome below is the script i used but doesn't do anything:/ using UnityEngine; using UnityEngine.SceneManagement;
public class Menu : MonoBehaviour {
public void StartGame ()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
SceneManager.LoadScene("ToonTown");
}
}
Have you tried to enable "Development build " in your Build settings and checking the logs of the built application?
C:\Users\username\AppData\LocalLow\CompanyName\ProductName\output_log.txt
Answer by bolkay · May 16, 2018 at 04:16 PM
Hi friend! First, this is something really easy to do. Second, you need to understand that it is necessary to have your scenes logically and sequentially arranged in build settings. For instance, scene (0) is the first scene that Unity loads upon pressing "play". The rest depends on you! With that in mind, in your StartGame() function. Use something like SceneManager.LoadScene(1); Of course, the above simple code assumes you want to load scene 1. Remember, Scene(0) is always the first one that Unity loads for you. The next simple thing to do is to attach the script that contains the function to an active GameObject in the hierarchy. In this case, just drag your script and attach it to Canvas. Now, head over to "Play" button, in the inspector, look for OnClick(). I would have supported you with pictures but I'm not with my laptop right now. Sorry. You will notice a little space where you can select something, drag your Canvas into that little area. Select "Editor and Runtime" Now, a kind of "drop down" would likely appear, you will select the StartGame() function therefrom.
Thank you i got it to work! Btw would you happen to know how to fix a UI health bar? $$anonymous$$ine closes in on its self ins$$anonymous$$d of going left to right, also i can't find a good tutorial for a "kill count" if you don't know how to do either of these thats fine:) Sorry if im asking too much
I found a good tutorial for you! https://www.mvcode.com/lessons/using-sliders-in-unity-aaron It's a very simple tutorial. Get you slider fixed!
Answer by KjipGamer · May 16, 2018 at 04:15 PM
As SoshJam pointed out, you need to remove the first SceneManager.LoadScene
from your code, as what you're doing currently is loading the same scene twice. Next, make sure that the button is hooked to the class, otherwise the button press will obviously not work.
Your answer
Follow this Question
Related Questions
When enter deathscene cursor disappear 0 Answers
How can I intercept "Play scene" event at code? 1 Answer
Move camera upon button press 0 Answers
I can't click to an UI button. 3 Answers
Door Between Scenes 3 Answers