There’s a problem: I have two buttons on the same scene, one goes to level 1 the other goes to the settings menu. The problem is, the settings button also goes to level 1
After I change the words “first level” to “settings” and the proper build index , it still goes to level 1 (I did attach the code to the settings button). idk what to change in the code to go to the proper scene.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class firstlevel : MonoBehaviour
{
public void FirstLevel()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 3);
}
}
The above code gets you to the first level scene after selecting “play”
Can you open File > Build Settings in the Unity Editor Menu and double check the “Scenes in build” there? You can see the build index on the right hand side of the list.
Then note that your current code adds 3 to the buildIndex of the current active scene to determine the target index that is being loaded. So if your main menu scene with the buttons has buildIndex 2, this would load the scene with the buildIndex 5 (2+3).
This could additionally be more complicated if there are already multiple scenes open when your main menu is displayed, then the scene with the name printed in bold in the hierarchy would be the active scene.
You could also set a halting point in the debugger on that line and examine what SceneManager.GetActiveScene().buildIndex returns when you click the button.
If that does not help, would it be an alternative for your to load the scenes via their name instead? SceneManager.LoadScene also accepts a string with the scene name, so you could just use
Wait both buttons call the same function? They both need their own function of course, else both buttons will always do the same thing (unless you add a parameter to your function).
One way to set it up would be:
Button “First Level” calls the function FirstLevel() when you click it. In FirstLevel() there is the line
SceneManager.LoadScene("Level1");
Button “Settings” calls the function Settings() when you click it. In Settings() there is the line
SceneManager.LoadScene("Settings");
(Assuming your scenes are named “Level1” & “Settings”, if not you can change the names in the quotes accordingly).
I used the same function though for my other scenes and it went in the correct order as was in the build settings. It seems like if you have a second button on the same scene is where it messes up then all of a sudden it doesn’t go to the scene number even though I did the buildIndex code right. I didnt need to use how you did it for anything,
If the LoadScene with the buildIndex is working for you, you don’t need to change it, that was just a suggestion in case working with the buildIndex would be too complicated.
I have just one more idea what could be wrong: When you created the settings button, did you change the setting button’s OnClick() event so it will point to its own script / function, and not to the script/function of the level button?
It could be that at the moment when you click on the settings button the code of the level button is being executed.