September 28, 2023


We’ll show you how to switch scenes by code in this Unity Scene Tutorial. You’ll learn how to manage Scene Visibility and multi-scene editing.

What we’ll cover:

  • Setting Scene visibility for GameObjects and their children
  • Turning Scene visibility on and off
  • Isolating selected GameObjects

We’ll explore the Scene divider menu for loaded and unloaded Scenes.

And we’ll do lots of baking:

  • Lightmaps with multiple Scenes
  • Navmesh data with multiple Scenes
  • Occlusion Culling data with multiple Scenes

Lesson 6 Video Transcript

Hey there everyone. Welcome back to another Unity tutorial. In the previous lesson, we added some UI to our game for the first time while creating this main menu of sorts.

In this lesson, I’m going to explain how to switch scenes via code so we can get off this main menu and press the play button, and enter a scene called level one.

Now, we’re not actually going to create much of a level here, but we will create the scene called level one. So let’s go ahead and do that.

Now, I’m going to actually try creating a scene in a different way this time because there are quite a few different ways. I’m going to press Ctrl N. As you can see, we now have an untitled scene. I’m going to add a cube to it just so that we know where we are. Put that at 0, 0, 0.

I’m going to double-click it to zoom in on it. And there we go. There’s the cube. I’m going to press control S. Go to the scenes folder and name this level one, one. So there we go. We have level one.

Now let’s switch back to the main menu. And here we’re going to find our button, the play button, and we want to make it so that when we press this, it will change scenes to level one.

Now there are two things that we need to know before we try to do this. The first one is about scenes. While we can double-click scenes to switch between them, the code actually has one extra step before it knows how to switch scenes. In unity.

If we press control shift B, we get this new menu called the build settings. Only scenes that are displayed here can be switched to by code, and what this does is it basically lets us build the game like an E X E if we were building this to send to a friend or to put on Steam or anything like that.

Of course, for PC, that’s what we have selected here. And when we do build it, it will only include scenes that are here. So for that same reason, we can only switch to scenes that are displayed in here. So why don’t we drag some in?

We can drag the main menu seen since that’s where we want to start. And as you can see, it’s index zero, meaning if we built it right now and sent the files to a friend, this is the scene that they would open up when they pressed play.

Now I’ll also drag level one in. So you can see level one is here and it’s index number one. Keeping that in mind, let’s finally create a script that will let us switch scenes over to level one.

So again, let’s select the button here and we’re going to add a component. So we have to scroll down since there are so many components on this. And we’re going to add a component. And I already have scene switcher type here. That’s exactly what I’m going to name it.

So I’m going to press enter and I’ll press enter one more time to create the script. And once it finishes compiling, it will add it to this object and we can double-click it to open in Visual Studio. So I’m

Going to that right now. Here we go.

The scene switcher is here. I’ll zoom in a little bit to make this easier to read. Now there are a few different things that go on here that can make this a bit complicated, so try your best to follow along and I’ll explain it bit by bit.

We only want this to happen when we press the button, so we don’t even need update anymore without start or update. We actually can create our own function.

So we’re going to create one, if you remember it was void. And let’s do play button pressed. You do open-close parentheses, and then open-close parentheses right here. And this will allow you to put any code that you’d like inside of these braces.

So with this function, if we also add the word public here, we’ll be able to attach it to our button and I’ll show you what that looks like in just a second.

So with this function here, public void, play button pressed, all we need is to switch scenes right here with some code. In order to do this, we first need to ask Unity for its scene management class. So we can do using Unity engine scene management.

Now that we have this at the top, we can type scene manager load scene, and we can either pass in one because that was the ID for level one, or we can pass in the name level one. Make sure you have a semicolon at the end of this line, press save so there’s no asterisk up here and switch back into Unity.

Now we’ve arrived at the final step.

Let’s take our button class here and click on the plus sign. Now you can see we can drag an object here and call a function. So let’s take our scene switcher, drag it right into this field, open the dropdown menu, go scene, switcher, and then play button pressed the function that we added. We can press save.

And now when we press play and we click on the button, once play mode is activated, we’ve switched scenes and it’s as simple as that. You could have multiple buttons to switch to multiple scenes for something like a level select, for example.

And that’s just the most basic example.

Leave a Reply

Your email address will not be published. Required fields are marked *