1 00:00:04,040 --> 00:00:04,910 ‫Welcome back. 2 00:00:04,940 --> 00:00:11,870 ‫In this video, we are going to add our scoring system and that what we'll need to do is to create a 3 00:00:11,870 --> 00:00:15,390 ‫new script which is called score controller. 4 00:00:15,410 --> 00:00:20,970 ‫So let's go ahead and go to our field because our field will be our score controller. 5 00:00:20,990 --> 00:00:24,870 ‫Game object or the game object that carries the score controller script. 6 00:00:24,890 --> 00:00:29,750 ‫So let's create a new script and call it score controller. 7 00:00:30,920 --> 00:00:31,280 ‫All right. 8 00:00:31,280 --> 00:00:33,680 ‫Let's go ahead and edit that script. 9 00:00:34,940 --> 00:00:37,910 ‫What we'll need in here are multiple things. 10 00:00:37,910 --> 00:00:43,370 ‫One is a information about how many points our player has. 11 00:00:43,370 --> 00:00:53,120 ‫So private end score player, I use a private integer here because it's not really something that is 12 00:00:53,120 --> 00:00:56,990 ‫relevant for our Unity editor, for example. 13 00:00:56,990 --> 00:01:00,200 ‫Then we need one for our player two. 14 00:01:00,200 --> 00:01:05,870 ‫So score player two and we set it to zero at the beginning. 15 00:01:06,170 --> 00:01:13,430 ‫Then we will need to game objects, which is one the score text of the player one and the score text 16 00:01:13,430 --> 00:01:14,480 ‫of the player two. 17 00:01:14,510 --> 00:01:21,140 ‫So what we need here is a public game object called score text player one. 18 00:01:26,190 --> 00:01:30,900 ‫And we copy that and duplicate it as score text player two. 19 00:01:32,580 --> 00:01:38,040 ‫Now we need another variable in order to know how many points we need in order to win. 20 00:01:38,040 --> 00:01:40,110 ‫So goals to win. 21 00:01:40,470 --> 00:01:42,690 ‫How I'm going to call it now. 22 00:01:42,690 --> 00:01:49,980 ‫What we need to do is to add the score of Player One or at the score of Player two with relevant methods. 23 00:01:49,980 --> 00:01:51,660 ‫So let's create two methods. 24 00:01:52,320 --> 00:02:06,030 ‫One is going to be a public void go player one, which is simply increasing the score of player one. 25 00:02:07,470 --> 00:02:14,190 ‫And we need the same thing for our Player two y public because we want to call those methods from outside 26 00:02:14,190 --> 00:02:15,390 ‫of this class. 27 00:02:15,750 --> 00:02:18,810 ‫So within other from other scripts. 28 00:02:19,560 --> 00:02:20,250 ‫All right. 29 00:02:22,440 --> 00:02:25,830 ‫So now we have those two methods. 30 00:02:25,830 --> 00:02:30,830 ‫We can go ahead and create our fixed update method and our update method. 31 00:02:30,840 --> 00:02:32,130 ‫Within our update method. 32 00:02:32,130 --> 00:02:37,560 ‫We want to constantly check if one of the two players has one and in the fixed update method we want 33 00:02:37,560 --> 00:02:39,420 ‫to update our UI. 34 00:02:39,450 --> 00:02:47,430 ‫So I'm going to use fixed update and here I'm going to update the UI. 35 00:02:47,730 --> 00:02:53,370 ‫If you want to access the text, you need to add a library here. 36 00:02:53,370 --> 00:03:00,960 ‫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 37 00:03:00,960 --> 00:03:11,220 ‫to create a text object which I call UI score player one, which is the score of the text, which is 38 00:03:11,220 --> 00:03:22,500 ‫this score text player one dot get component, which is the text, and then we can go ahead and set 39 00:03:22,500 --> 00:03:24,120 ‫that one UI score. 40 00:03:24,120 --> 00:03:28,440 ‫Player One text is equal to this dot score. 41 00:03:28,440 --> 00:03:37,950 ‫Player 1.2 string, which means whichever value we have for score player one, that value will be represented 42 00:03:37,950 --> 00:03:40,620 ‫in the text of our UI. 43 00:03:40,650 --> 00:03:44,640 ‫So that means in this text here, this zero here. 44 00:03:45,810 --> 00:03:46,530 ‫So the score. 45 00:03:46,530 --> 00:03:52,500 ‫Player one All right, now we need to do the same thing for our score play too. 46 00:03:52,620 --> 00:03:54,450 ‫So let's copy that. 47 00:03:56,600 --> 00:04:01,880 ‫And duplicate it and simply change the values from 1 to 2. 48 00:04:03,390 --> 00:04:09,780 ‫So it's really important that we are very careful here that all of them are changed correctly here as 49 00:04:09,780 --> 00:04:10,350 ‫well. 50 00:04:10,500 --> 00:04:13,200 ‫Score text player to get component text. 51 00:04:13,290 --> 00:04:21,120 ‫So now that we have changed those, we can go ahead and check if one of the two players has one, because 52 00:04:21,120 --> 00:04:24,540 ‫if one of the two players has one, we want to for now just debug that. 53 00:04:24,540 --> 00:04:25,770 ‫One of the players has one. 54 00:04:25,770 --> 00:04:40,890 ‫So if this dot score player one is higher, then this goes to win or this DOT score player two is higher 55 00:04:40,890 --> 00:04:41,460 ‫than this. 56 00:04:41,460 --> 00:04:44,250 ‫The goal is to win or goal to win. 57 00:04:45,060 --> 00:04:57,060 ‫Then we want to debug that log game one and usually we would want to move over to the next scene. 58 00:04:57,450 --> 00:05:04,440 ‫So now that we have that, we need to edit other scripts so that the score is increased at all in other 59 00:05:04,440 --> 00:05:08,040 ‫methods, so that the score is increased at all. 60 00:05:08,040 --> 00:05:09,750 ‫And when should it be increased? 61 00:05:10,110 --> 00:05:12,810 ‫Well, in our collision script. 62 00:05:12,810 --> 00:05:19,740 ‫So let's drag that score controller script into our script and go to our collision controller script 63 00:05:19,770 --> 00:05:20,700 ‫in here. 64 00:05:20,700 --> 00:05:25,830 ‫Whenever the left wall is hit, we want to increase the score of player two. 65 00:05:25,830 --> 00:05:29,280 ‫So we will need a score controller object here. 66 00:05:29,280 --> 00:05:36,390 ‫So public score controller, I'm going to call it Score Controller. 67 00:05:39,640 --> 00:05:45,610 ‫So now we can say this dot score controller, dot goal player too. 68 00:05:48,070 --> 00:05:52,690 ‫So that means if the left ball is hit, so the ball hits the left wall. 69 00:05:52,720 --> 00:05:54,490 ‫Player two gets a point. 70 00:05:54,970 --> 00:05:58,270 ‫And if the right ball is hit, player one gets a point. 71 00:05:58,280 --> 00:06:00,340 ‫So this dot score. 72 00:06:00,340 --> 00:06:03,130 ‫Controller Dot go. 73 00:06:03,160 --> 00:06:04,960 ‫Player one. 74 00:06:05,740 --> 00:06:07,720 ‫All right, now let's test that. 75 00:06:08,830 --> 00:06:12,370 ‫Let's see if the scoring does anything. 76 00:06:12,370 --> 00:06:17,830 ‫And if we do that, we should get an error because we have not assigned the script yet. 77 00:06:18,250 --> 00:06:19,030 ‫Let's have a chat. 78 00:06:19,090 --> 00:06:20,020 ‫Let's have a look. 79 00:06:22,480 --> 00:06:23,350 ‫Enfield. 80 00:06:23,350 --> 00:06:27,730 ‫We have no score texts, so let's drag the score. 81 00:06:27,730 --> 00:06:32,260 ‫PLAYER one and the score player two in here and then go to win. 82 00:06:32,260 --> 00:06:36,550 ‫Let's say five points is the goal in order to win. 83 00:06:36,850 --> 00:06:38,290 ‫So let's go ahead. 84 00:06:40,140 --> 00:06:43,740 ‫And see if we get points at all. 85 00:06:47,010 --> 00:06:49,500 ‫So we get another point reference. 86 00:06:55,710 --> 00:06:58,530 ‫Because our ball does not have the score controller yet. 87 00:06:58,530 --> 00:07:00,420 ‫So let's drag that score controller. 88 00:07:00,420 --> 00:07:04,170 ‫So that means we drag our field in there. 89 00:07:04,170 --> 00:07:06,870 ‫So as soon as we have done that, let's check it out again. 90 00:07:12,030 --> 00:07:13,170 ‫So our ball. 91 00:07:13,170 --> 00:07:15,240 ‫Let's leave it through. 92 00:07:15,420 --> 00:07:15,870 ‫All right. 93 00:07:15,990 --> 00:07:18,270 ‫You are collusion with wall right? 94 00:07:18,270 --> 00:07:19,770 ‫Collusion with wall left. 95 00:07:19,840 --> 00:07:22,230 ‫As you can see, our score increases already. 96 00:07:22,560 --> 00:07:26,280 ‫And now let's see if we get towards five at one point. 97 00:07:40,780 --> 00:07:41,890 ‫And one more. 98 00:07:41,890 --> 00:07:42,970 ‫One last thing. 99 00:07:43,180 --> 00:07:47,230 ‫Last point, you see that we have five and we see game one. 100 00:07:47,680 --> 00:07:51,430 ‫By the way, what you see here is a rotation of our ball. 101 00:07:51,430 --> 00:07:53,550 ‫So we could change that as well. 102 00:07:53,560 --> 00:07:58,420 ‫So let's freeze the rotation for our Z. 103 00:08:00,250 --> 00:08:02,500 ‫FA the value of our ball. 104 00:08:03,060 --> 00:08:09,400 ‫Okay, so now that we know that we can get points, what we need to do as well is to reset the game 105 00:08:09,850 --> 00:08:12,850 ‫and that's something that we are going to do in the next game. 106 00:08:12,850 --> 00:08:15,340 ‫So we're going to check out how to reset the game. 107 00:08:15,340 --> 00:08:18,940 ‫And we also need to add our game over screen. 108 00:08:18,940 --> 00:08:22,060 ‫So let's save the game and see you in the next video.