Open an existing or a new project. Let’s create a new scene so that we have two scenes available.
We have to add our scenes in the Build Settings: File > Build Settings. This keeps track of our scenes and issues an index number to them.
Notice how there are numbers on the scene list?
Each scene gets assign an index number that we can reference through code.
Let’s create a UI button to imitate a “Load New Game” feature and change the text to “Load Scene.”
Update the settings in the inspector. Customize your button as you wish.
We’ll position the button to the center of the screen.
We want to be able to load into the other scene when we press the Load Scene button.
So we need to create logic through code.
Let’s create a GameManager script and attach it to a new game object called “Game Manager” in the hierarchy.
Open the GameManager script to add a function for our button we created. This function will add logic to change scenes when the player clicks on the button.
We need to add a namespace that will give us access to the SceneManagement library.
Now we can create our function.
public void LoadScene()
SceneManager.LoadScene(1); //the index of the seconf scene
We made it public so that our button can have access to it from the inspector.
Save your script and head back to Unity.
We need to select our Button. Click on the plus sign on “On Click ()” under the Button component. Drag the Game Manager object from the hierarchy into the game object slot.
Remember, we attached the GameManager script to it where the
LoadScene() function is.
We then need to select the function we created in the GameManager script. Down below is a walkthrough for visualization.
We now have a working button that can load into our second scene!
There is more to it than just a button, but these are the fundamentals to load a scene in Unity!
That is all for today! Thank you for reading! :)