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
    
      
  Implementing a simple score system is a great way to improve this game. In this video, we’ll see just how easily it can be accomplished.
Unity 6 Documentation
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
                      Welcome back everyone.
                      0:04
                    
                    
                      At this point,
we have a functioning and looping game.
                      0:06
                    
                    
                      This is fantastic.
                      0:09
                    
                    
                      But we need some kind of reward
for the player, or
                      0:11
                    
                    
                      this will not give any incentive
for them to play more than once.
                      0:13
                    
                    
                      What we need is a score system
to let them know
                      0:17
                    
                    
                      how far they've gotten in their run.
                      0:19
                    
                    
                      This will give them a goal
to try and beat the next time they play.
                      0:21
                    
                    
                      The idea
                      0:25
                    
                    
                      is that every time they make it through
a set of pipes we'll increment their score.
                      0:26
                    
                    
                      So as I mentioned before,
                      0:30
                    
                    
                      the GameManager script is a great place
to keep track of this.
                      0:31
                    
                    
                      Let's open it up.
                      0:34
                    
                    
                      This is going to be very simple.
                      0:36
                    
                    
                      We need a variable to keep track
of the current score, a method to increase
                      0:38
                    
                    
                      the score, and a way of detecting
when this method should be called.
                      0:42
                    
                    
                      So let's start by creating our variable.
                      0:47
                    
                    
                      We'll only be using whole numbers
with our score,
                      0:49
                    
                    
                      so let's make it an integer type.
                      0:51
                    
                    
                      private int currentScore, and
                      0:54
                    
                    
                      let's give it
                      0:59
                    
                    
                      a default value of 0,
so that they don't start with anything.
                      0:59
                    
                    
                      For our method,
we'll want it to be public again,
                      1:03
                    
                    
                      since we'll be calling this
from our Player script.
                      1:05
                    
                    
                      So let's go below our ReloadScene
and say public void
                      1:08
                    
                    
                      IncrementScore.
                      1:12
                    
                    
                      I'm going to give the player
5 points every time
                      1:15
                    
                    
                      they make it through a set of pipes
but you can use any number that you like.
                      1:17
                    
                    
                      Now I know I've kind of broken this rule
a few times throughout this course,
                      1:22
                    
                    
                      but it was me trying not to put
too many things in your brain all at once.
                      1:25
                    
                    
                      But typically, it's
not a great practice to hard code
                      1:29
                    
                    
                      what some people call magic numbers
like this in your code.
                      1:32
                    
                    
                      So let's go make a variable up
top real quick.
                      1:36
                    
                    
                      And actually,
why don't you give it a shot?
                      1:38
                    
                    
                      What we need is an integer variable
                      1:41
                    
                    
                      for the game to know 
how much score to add each time.
                      1:44
                    
                    
                      Go ahead and pause me and give it a go.
                      1:47
                    
                    
                      How'd you do?
                      1:51
                    
                    
                      Here's my approach.
                      1:52
                    
                    
                      I'll serialize it, just in case I want to
change it later in the inspector.
                      1:54
                    
                    
                      It'll be private, type int,
                      1:58
                    
                    
                      and I'll just name it scoreToAdd.
                      2:01
                    
                    
                      I'll add this
                      2:06
                    
                    
                      to our method instead of my 5,
and for now,
                      2:06
                    
                    
                      let's add a log to the console
to make sure this is working as intended.
                      2:09
                    
                    
                      Okay,
                      2:18
                    
                    
                      let's save the file
and head back to Unity.
                      2:19
                    
                    
                      To detect when the player has entered
                      2:22
                    
                    
                      or exited a given space,
we can use colliders.
                      2:24
                    
                    
                      Since we want this moving along with
each set of pipes let's add it to our prefab.
                      2:28
                    
                    
                      So let's double click it from the
project window to open our prefab
                      2:33
                    
                    
                      editor
                      2:36
                    
                    
                      Let's right click the parent pipes
                      2:41
                    
                    
                      object and create an empty child
game object and name it score box.
                      2:42
                    
                    
                      I'm going to give this a
                      2:49
                    
                    
                      box collider 2D component.
                      2:50
                    
                    
                      Let's grab the Move tool with W
                      2:57
                    
                    
                      and place it right between the pipes.
                      2:59
                    
                    
                      Ok, let's enable
                      3:07
                    
                    
                      our Edit Collider option here
and let's make it slightly thinner
                      3:08
                    
                    
                      than the outside of the pipes and stretch
it vertically to fit the whole space.
                      3:11
                    
                    
                      Perfect.
                      3:20
                    
                    
                      Now as this is, if the player
                      3:22
                    
                    
                      were to try and fly through here,
they're going to hit this invisible box.
                      3:23
                    
                    
                      To avoid this, and to allow them
to pass through unaffected,
                      3:27
                    
                    
                      we enable this isTrigger option.
                      3:31
                    
                    
                      Now they'll pass right through it,
and we'll be able to tell in our code.
                      3:34
                    
                    
                      Let's back out of this
and open up our Player script.
                      3:38
                    
                    
                      To detect when an
object has come into contact
                      3:43
                    
                    
                      with a collider marked as a trigger,
                      3:45
                    
                    
                      there's a method just like our OnCollisionEnter2D.
It's called OnTriggerEnter2D.
                      3:48
                    
                    
                      This receives data from the collision
the same as before.
                      3:56
                    
                    
                      I'll just leave it as collision for now
as I'm not too worried about this one.
                      3:59
                    
                    
                      And I'm not going to be adding
any other triggers in this game,
                      4:03
                    
                    
                      but if you're going to, you'll
want to give that score box object
                      4:06
                    
                    
                      a new tag and create a conditional
like we did above.
                      4:09
                    
                    
                      But since in my case,
I know the player won't be hitting
                      4:13
                    
                    
                      any other triggers, I'm
just going to write the logic right here.
                      4:16
                    
                    
                      Also, since we already have a reference
to our GameManager
                      4:20
                    
                    
                      and our score method is public,
it should show right up for us.
                      4:23
                    
                    
                      GameManager.IncrementScore.
                      4:27
                    
                    
                      There it is.
                      4:29
                    
                    
                      Awesome.
                      4:30
                    
                    
                      Let's save this and test it in Unity.
                      4:31
                    
                    
                      Great.
                      4:43
                    
                    
                      We can see the score getting
updated in our console.
                      4:44
                    
                    
                      And if I lose and restart, yep,
                      4:51
                    
                    
                      the score resets as well, as you can see
with this counter on the right.
                      4:54
                    
                    
                      Perfect.
                      4:59
                    
                    
                      In the next video, we'll introduce how
we work with text and UI elements in Unity
                      5:00
                    
                    
                      and give the player
visual feedback on their current score.
                      5:04
                    
                    
                      See you there.
                      5:07
                    
              
        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