Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed Unity Basics!
      
    
You have completed Unity Basics!
Preview
    
      
  Now that we know when the player hits a pipe, let’s reload the game so that they can start over.
Unity 6 Documentation
Resources
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
                      We're now detecting
                      0:01
                    
                    
                      collisions on our player
and singling out collisions with pipes.
                      0:02
                    
                    
                      Now we need to end the run for the player
so that they can't keep playing forever.
                      0:06
                    
                    
                      For now, I'm
going to simply reload the scene
                      0:10
                    
                    
                      so that they immediately start over.
                      0:12
                    
                    
                      It's going to feel a bit abrupt,
but in the next stage
                      0:14
                    
                    
                      we're going to polish up our game
and we'll implement a nice simple menu
                      0:17
                    
                    
                      for the player to restart
when they're ready.
                      0:21
                    
                    
                      Now we could add this scene
reloading logic
                      0:23
                    
                    
                      directly in our OnCollisionEnter2D
method in the Player script.
                      0:26
                    
                    
                      But I was thinking ahead a bit,
                      0:30
                    
                    
                      and I think it's time to create
an overall game manager script.
                      0:31
                    
                    
                      These are pretty common in games
and handle overall game logic
                      0:35
                    
                    
                      that doesn't need to be, or shouldn't be,
attached to a specific game object,
                      0:39
                    
                    
                      similar to our pipes manager.
                      0:43
                    
                    
                      This can eventually
handle our score as well.
                      0:45
                    
                    
                      So let's create a new
                      0:48
                    
                    
                      empty game object and name
it game manager.
                      0:49
                    
                    
                      I'm going to reset the transform
                      0:56
                    
                    
                      and move it up top before.
                      0:57
                    
                    
                      Let's go into our scripts
folder and create a new script
                      1:01
                    
                    
                      also named GameManager.
                      1:03
                    
                    
                      Finally, let's attach this script
                      1:15
                    
                    
                      to our new object
and open it up in our IDE.
                      1:17
                    
                    
                      Alright,
whenever we need to write code that works
                      1:21
                    
                    
                      with our scenes in Unity,
we need to use a specific namespace.
                      1:23
                    
                    
                      I'm going to go right beneath
our UnityEngine using directive and write
                      1:28
                    
                    
                      using UnityEngine.SceneManagement
and put in our semicolon.
                      1:31
                    
                    
                      This gives us access to a variety of tools
for working with our scenes.
                      1:37
                    
                    
                      To handle our scene reload, let's create
a custom method beneath Update.
                      1:42
                    
                    
                      private, void,
and let's name it ReloadScene.
                      1:46
                    
                    
                      In here, let's start typing SceneManager, dot,
                      1:54
                    
                    
                      and the method we want is LoadScene.
                      1:59
                    
                    
                      If we hover over it,
we see it states that it loads
                      2:05
                    
                    
                      the scene by its name
or index in the build settings.
                      2:08
                    
                    
                      You're probably wondering
what in the world build settings are.
                      2:12
                    
                    
                      Let's comment this out,
save the file, and head back to Unity.
                      2:16
                    
                    
                      If we go to File,
we'll see an option for Build Profiles.
                      2:21
                    
                    
                      Let's select that.
                      2:24
                    
                    
                      This is where we configure
most things for our game
                      2:26
                    
                    
                      when we create a build of it
that we can play on our machines.
                      2:28
                    
                    
                      You can see here is where we can change
the platform that we want to build it for.
                      2:32
                    
                    
                      I'm running Unity on Windows,
so it has this selected by default.
                      2:36
                    
                    
                      This is where you'll see the modules
installed to this version of Unity
                      2:40
                    
                    
                      that we did way back at the beginning.
                      2:43
                    
                    
                      So if you go install, say, the iOS module
                      2:45
                    
                    
                      and reopen this project,
it should be available in this list.
                      2:48
                    
                    
                      At the top of this list is Scene List.
                      2:51
                    
                    
                      Click on that.
                      2:53
                    
                    
                      Here we have what would be
a list of scenes that we've created
                      2:55
                    
                    
                      in our game that we want to include
in our final build.
                      2:58
                    
                    
                      We have SampleScene,
which Unity has added in automatically.
                      3:01
                    
                    
                      This is the scene created for us
when we created this project.
                      3:05
                    
                    
                      It's also the one we've been working
in, as you can see at the top of
                      3:08
                    
                    
                      the hierarchy here too.
                      3:11
                    
                    
                      Over on the right is our scene's index.
                      3:14
                    
                    
                      This one is 0,
because it's a zedo-based index.
                      3:16
                    
                    
                      Now, what I'm about to do isn't necessary,
just for demonstration.
                      3:20
                    
                    
                      I'm going to close this, go to the Scenes
folder in our project window,
                      3:24
                    
                    
                      and rename this to, say, GameScene.
                      3:27
                    
                    
                      Going back to the scene list,
                      3:34
                    
                    
                      you can see it's been updated,
as well as the top of the hierarchy.
                      3:35
                    
                    
                      Okay, let's close this out
and go back to our script.
                      3:39
                    
                    
                      So since we know that our game
will only have this one scene, we could
                      3:43
                    
                    
                      safely put zero in here as an argument
and this will work perfectly.
                      3:47
                    
                    
                      But what if we had
multiple scenes in our game?
                      3:51
                    
                    
                      I'd to show you a convenient way
of getting the current scene's
                      3:53
                    
                    
                      index in our code
so that this ReloadScene
                      3:56
                    
                    
                      method would work no matter what
level or menu the player would be on.
                      3:59
                    
                    
                      This is super useful in bigger games.
                      4:03
                    
                    
                      Let's save it to a new variable
to keep things readable.
                      4:06
                    
                    
                      So above this line, let's create an integer
variable and name it currentIndex.
                      4:09
                    
                    
                      We'll set it equal to SceneManager,
and what we want is GetActiveScene.
                      4:13
                    
                    
                      We'll see it's throwing an error.
                      4:25
                    
                    
                      If we hover over it, it says it
cannot convert type Scene to int.
                      4:27
                    
                    
                      This is because we're saying our variable
here is an integer,
                      4:31
                    
                    
                      but this GetActiveScene
method returns a scene object.
                      4:34
                    
                    
                      Lucky for us, there's a property we can
get off of a scene object to help us out.
                      4:38
                    
                    
                      After the parentheses,
we'll type .buildIndex.
                      4:42
                    
                    
                      This returns the index of the scene
in the build settings.
                      4:47
                    
                    
                      Perfect!
                      4:50
                    
                    
                      Now we just plug this into our LoadScene
method.
                      4:51
                    
                    
                      So to break this down,
we're creating an integer variable.
                      4:54
                    
                    
                      We're calling the GetActiveScene
method off of the SceneManager class,
                      4:58
                    
                    
                      which comes from the SceneManagement
namespace.
                      5:02
                    
                    
                      I know, right?
                      5:06
                    
                    
                      Then, we're getting the buildIndex value
of that active scene, which is an integer.
                      5:08
                    
                    
                      Then, we're telling Unity to load a scene
                      5:13
                    
                    
                      and providing it the index of the scene
that we want to load.
                      5:15
                    
                    
                      In our case, that is the scene
the player is currently playing.
                      5:19
                    
                    
                      Okay, now we need to call this method
                      5:23
                    
                    
                      at the right moment,
so let's head back into our player script.
                      5:25
                    
                    
                      In order to call a method from
another script, we need a reference to it,
                      5:29
                    
                    
                      just we need reference
                      5:33
                    
                    
                      to other game objects or components
in order to interact with them.
                      5:34
                    
                    
                      So up at the top, let's create a variable.
                      5:38
                    
                    
                      This will be a private
                      5:42
                    
                    
                      of type GameManager, since
that's what we named that class,
                      5:44
                    
                    
                      and let's name
it gameManager in camel case.
                      5:47
                    
                    
                      Down in Start, let's assign it a value.
                      5:53
                    
                    
                      We could have serialized this field
and drag and dropped our game manager
                      5:55
                    
                    
                      object into the slot,
                      5:59
                    
                    
                      but I wanted to show you
how to reference components
                      6:01
                    
                    
                      that are not attached to the same game
object in code.
                      6:03
                    
                    
                      To do this, we type gameManager equals,
                      6:06
                    
                    
                      then start typing Find.
                      6:10
                    
                    
                      We can see we have two options here
FindAnyObjectByType
                      6:12
                    
                    
                      or FindFirstObjectByType. They'll both work
for us in this case as there's
                      6:16
                    
                    
                      only one game manager in our scene.
So let just use find first.
                      6:20
                    
                    
                      Just like GetComponent above,
we need to provide a type
                      6:25
                    
                    
                      within some angle brackets
so that it knows what to look for.
                      6:28
                    
                    
                      So here we'll write in GameManager again.
                      6:31
                    
                    
                      So now,
                      6:37
                    
                    
                      down in our OnCollisionEnter,
we can replace our log
                      6:37
                    
                    
                      with a call to 
gameManager.ReloadScene.
                      6:40
                    
                    
                      But, uh-oh, we're getting an error here.
                      6:48
                    
                    
                      Let's see what it says.
                      6:50
                    
                    
                      GameManager.ReloadScene is inaccessible
due to its protection level.
                      6:52
                    
                    
                      Ahh... so here is our first occurrence
where we need to actually make
                      6:57
                    
                    
                      something public, not private,
                      7:01
                    
                    
                      because we're trying to access it from
outside of the script it's written in.
                      7:03
                    
                    
                      So let's go back to our GameManager
script,
                      7:07
                    
                    
                      and let's change
this method from private to public,
                      7:10
                    
                    
                      and save.
                      7:14
                    
                    
                      Now our error is gone in
the Player script!
                      7:17
                    
                    
                      Also, if I delete this method call
and start typing it again, you'll see
                      7:20
                    
                    
                      since it's public, it actually shows up
in our IntelliSense options.
                      7:24
                    
                    
                      Awesome!
                      7:28
                    
                    
                      Let's save and test this out.
                      7:29
                    
                    
                      I'm going to change my game window
                      7:31
                    
                    
                      to play maximized,
but you don't need to follow me here.
                      7:35
                    
                    
                      There we go.
                      7:46
                    
                    
                      Anytime we come into contact with a pipe,
the game is reloaded.
                      7:47
                    
                    
                      Let's take a moment to reflect on
everything we've done so far.
                      7:51
                    
                    
                      We've created game objects
                      7:55
                    
                    
                      and manipulated them during runtime
with our own custom code.
                      7:56
                    
                    
                      We've detected collisions
and reacted to them.
                      8:00
                    
                    
                      We even created a loop
where the player restarts when they lose.
                      8:03
                    
                    
                      If you think about it, technically,
you've created a game.
                      8:07
                    
                    
                      Congratulations, game dev.
                      8:11
                    
                    
                      You should be pretty proud of yourself.
                      8:13
                    
                    
                      Now, it doesn't look the best,
and there's a few other small things
                      8:15
                    
                    
                      we can add to really make this
a more enjoyable experience to the player.
                      8:18
                    
                    
                      So in the next stage,
we're gonna polish our game.
                      8:22
                    
                    
                      I'll see you there.
                      8:25
                    
              
        You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up