‫Welcome back.
‫In this video, we are going to add our scoring system and that what we'll need to do is to create a
‫new script which is called score controller.
‫So let's go ahead and go to our field because our field will be our score controller.
‫Game object or the game object that carries the score controller script.
‫So let's create a new script and call it score controller.
‫All right.
‫Let's go ahead and edit that script.
‫What we'll need in here are multiple things.
‫One is a information about how many points our player has.
‫So private end score player, I use a private integer here because it's not really something that is
‫relevant for our Unity editor, for example.
‫Then we need one for our player two.
‫So score player two and we set it to zero at the beginning.
‫Then we will need to game objects, which is one the score text of the player one and the score text
‫of the player two.
‫So what we need here is a public game object called score text player one.
‫And we copy that and duplicate it as score text player two.
‫Now we need another variable in order to know how many points we need in order to win.
‫So goals to win.
‫How I'm going to call it now.
‫What we need to do is to add the score of Player One or at the score of Player two with relevant methods.
‫So let's create two methods.
‫One is going to be a public void go player one, which is simply increasing the score of player one.
‫And we need the same thing for our Player two y public because we want to call those methods from outside
‫of this class.
‫So within other from other scripts.
‫All right.
‫So now we have those two methods.
‫We can go ahead and create our fixed update method and our update method.
‫Within our update method.
‫We want to constantly check if one of the two players has one and in the fixed update method we want
‫to update our UI.
‫So I'm going to use fixed update and here I'm going to update the UI.
‫If you want to access the text, you need to add a library here.
‫So I'm going to add or a namespace, I'm going to add UI so I can access the text class and I'm going
‫to create a text object which I call UI score player one, which is the score of the text, which is
‫this score text player one dot get component, which is the text, and then we can go ahead and set
‫that one UI score.
‫Player One text is equal to this dot score.
‫Player 1.2 string, which means whichever value we have for score player one, that value will be represented
‫in the text of our UI.
‫So that means in this text here, this zero here.
‫So the score.
‫Player one All right, now we need to do the same thing for our score play too.
‫So let's copy that.
‫And duplicate it and simply change the values from 1 to 2.
‫So it's really important that we are very careful here that all of them are changed correctly here as
‫well.
‫Score text player to get component text.
‫So now that we have changed those, we can go ahead and check if one of the two players has one, because
‫if one of the two players has one, we want to for now just debug that.
‫One of the players has one.
‫So if this dot score player one is higher, then this goes to win or this DOT score player two is higher
‫than this.
‫The goal is to win or goal to win.
‫Then we want to debug that log game one and usually we would want to move over to the next scene.
‫So now that we have that, we need to edit other scripts so that the score is increased at all in other
‫methods, so that the score is increased at all.
‫And when should it be increased?
‫Well, in our collision script.
‫So let's drag that score controller script into our script and go to our collision controller script
‫in here.
‫Whenever the left wall is hit, we want to increase the score of player two.
‫So we will need a score controller object here.
‫So public score controller, I'm going to call it Score Controller.
‫So now we can say this dot score controller, dot goal player too.
‫So that means if the left ball is hit, so the ball hits the left wall.
‫Player two gets a point.
‫And if the right ball is hit, player one gets a point.
‫So this dot score.
‫Controller Dot go.
‫Player one.
‫All right, now let's test that.
‫Let's see if the scoring does anything.
‫And if we do that, we should get an error because we have not assigned the script yet.
‫Let's have a chat.
‫Let's have a look.
‫Enfield.
‫We have no score texts, so let's drag the score.
‫PLAYER one and the score player two in here and then go to win.
‫Let's say five points is the goal in order to win.
‫So let's go ahead.
‫And see if we get points at all.
‫So we get another point reference.
‫Because our ball does not have the score controller yet.
‫So let's drag that score controller.
‫So that means we drag our field in there.
‫So as soon as we have done that, let's check it out again.
‫So our ball.
‫Let's leave it through.
‫All right.
‫You are collusion with wall right?
‫Collusion with wall left.
‫As you can see, our score increases already.
‫And now let's see if we get towards five at one point.
‫And one more.
‫One last thing.
‫Last point, you see that we have five and we see game one.
‫By the way, what you see here is a rotation of our ball.
‫So we could change that as well.
‫So let's freeze the rotation for our Z.
‫FA the value of our ball.
‫Okay, so now that we know that we can get points, what we need to do as well is to reset the game
‫and that's something that we are going to do in the next game.
‫So we're going to check out how to reset the game.
‫And we also need to add our game over screen.
‫So let's save the game and see you in the next video.