1
00:00:03,860 --> 00:00:09,320
Before we start worrying about the look of our future UI, let's first implement a scoring system so

2
00:00:09,320 --> 00:00:14,480
the player can see how well they're doing in the game and also make a couple of tweaks to our health

3
00:00:14,480 --> 00:00:17,420
script so that we can show the player their current health.

4
00:00:17,720 --> 00:00:23,240
And because I think you're completely up to the task now, this is going to be a rather large challenge

5
00:00:23,240 --> 00:00:23,900
for you.

6
00:00:24,140 --> 00:00:30,350
So I want you to go ahead and write a public getter method in our health script to return the current

7
00:00:30,350 --> 00:00:31,550
health of the player.

8
00:00:31,790 --> 00:00:36,920
And then I want you to write a scorekeeper script which will attach to an object in the hierarchy.

9
00:00:37,010 --> 00:00:42,680
This script will have a private variable to store the current score, and because this variable will

10
00:00:42,680 --> 00:00:47,960
be private, we're going to need a public getter method to return this current score so we can send

11
00:00:47,960 --> 00:00:49,250
that off to our UI.

12
00:00:49,280 --> 00:00:54,860
Later on, we're going to need a public method to modify that score, and we're also going to want a

13
00:00:54,860 --> 00:00:57,260
public method to reset that score.

14
00:00:57,650 --> 00:01:02,970
And finally, for this challenge, we want to increase the score whenever an enemy is destroyed.

15
00:01:02,990 --> 00:01:05,420
So that, again, will be in our health script.

16
00:01:05,540 --> 00:01:10,280
So a pretty large challenge here, but pause the video and give it your best shot.

17
00:01:15,720 --> 00:01:16,170
Okay.

18
00:01:16,170 --> 00:01:16,890
Welcome back.

19
00:01:16,890 --> 00:01:22,230
And a very big congratulations if you managed to do every single part of that challenge, if you didn't

20
00:01:22,230 --> 00:01:24,570
know, not to worry, we're going to go through it now.

21
00:01:24,570 --> 00:01:29,010
So the first part of this challenge was to make a public get a method for our health.

22
00:01:29,010 --> 00:01:33,630
So if we jump into our health script, we have our private health here.

23
00:01:33,630 --> 00:01:38,850
So let's just go and make a new public method and I'm going to put that underneath my on trigger enter.

24
00:01:38,850 --> 00:01:43,890
So I've got all of my unity built in methods together, my private methods together, and now our public

25
00:01:43,890 --> 00:01:44,610
methods.

26
00:01:44,610 --> 00:01:49,080
This is going to have to return an integer and we're going to call it Get Health.

27
00:01:49,590 --> 00:01:54,000
And this is a very simple getter, so it's just going to return our health.

28
00:01:54,300 --> 00:01:57,030
So that's the first part of the challenge done.

29
00:01:57,030 --> 00:02:00,660
And now if we head into unity, we're going to create our scorekeeper.

30
00:02:00,690 --> 00:02:07,080
Let's right click in the hierarchy, create a new empty game object and reset its position.

31
00:02:07,650 --> 00:02:10,650
Let's call this game object the scorekeeper.

32
00:02:11,270 --> 00:02:16,200
And we're going to create a new C-sharp script to attach to this called The Scorekeeper.

33
00:02:17,370 --> 00:02:22,320
Let's drag our script onto our object and then open that up for this.

34
00:02:22,320 --> 00:02:27,720
We're not going to need our start and our update methods, but we are going to need a private variable

35
00:02:27,720 --> 00:02:30,540
of type INT to hold our score.

36
00:02:30,990 --> 00:02:34,230
Because this is private, we're going to need a public getter for this.

37
00:02:34,230 --> 00:02:42,150
So we'll have a public int get score and this will just return our score with that set.

38
00:02:42,150 --> 00:02:44,760
Let's give ourselves a way to modify this score.

39
00:02:44,760 --> 00:02:52,170
So this time we're going to have a public void called Modify Score, and this is going to accept a parameter

40
00:02:52,170 --> 00:02:54,660
of type int called value.

41
00:02:54,660 --> 00:03:01,350
So how much we want to modify this score by and then to modify our score or we need to say is score

42
00:03:01,380 --> 00:03:03,810
plus equals our value.

43
00:03:04,620 --> 00:03:09,450
Now we could get away with leaving this method as it is, but I'm going to do a little bit of extra

44
00:03:09,450 --> 00:03:13,110
work to clamp my score so it doesn't go below zero.

45
00:03:13,320 --> 00:03:16,380
So for this we can say math f clamp.

46
00:03:16,860 --> 00:03:18,840
We want to clamp our score.

47
00:03:18,870 --> 00:03:23,250
We don't want it to go below zero and we don't care how high it goes.

48
00:03:23,250 --> 00:03:26,310
So we can say int dot max value.

49
00:03:26,880 --> 00:03:30,270
Finally, for this method, we wanted a way to reset our score.

50
00:03:30,270 --> 00:03:32,100
So setting it back to zero.

51
00:03:32,100 --> 00:03:37,980
So again we'll create a public method of type void and call this one reset score.

52
00:03:38,730 --> 00:03:42,660
And this method just has to set our score equal to zero.

53
00:03:43,080 --> 00:03:46,230
So congratulations if you manage to write all of that yourself.

54
00:03:46,230 --> 00:03:51,150
And the last step of your challenge was to increment the score whenever we killed an enemy.

55
00:03:51,270 --> 00:03:57,060
So if we jump back over to our health script right at the top, we're going to need a reference to our

56
00:03:57,060 --> 00:03:57,990
scorekeeper.

57
00:03:58,020 --> 00:04:02,100
So we'll call this the scorekeeper in Awake.

58
00:04:02,100 --> 00:04:07,950
We then need to find our scorekeeper so we can use find object of type scorekeeper.

59
00:04:09,800 --> 00:04:16,010
And because we're using this whole script on both our player and our enemies, we want a way of separating

60
00:04:16,010 --> 00:04:16,610
these outs.

61
00:04:16,610 --> 00:04:19,130
We're not adding a score when the player dies.

62
00:04:19,310 --> 00:04:24,830
So let's write at the top at a new serialised field of type bull and we'll call.

63
00:04:24,830 --> 00:04:26,120
This is Player.

64
00:04:26,660 --> 00:04:30,560
This will also have a use later on, so it's quite nice that we're adding it now.

65
00:04:30,830 --> 00:04:36,140
Finally, we're going to need to give these things a skull so we could potentially use the base health

66
00:04:36,140 --> 00:04:36,650
for this.

67
00:04:36,650 --> 00:04:41,430
But I think it would be quite nice if we had a independent variable for the score alone.

68
00:04:41,450 --> 00:04:47,840
So this is going to be type int and we'll call it the score and we can set this equal to maybe 50 to

69
00:04:47,840 --> 00:04:49,550
mirror our default health.

70
00:04:49,550 --> 00:04:54,620
But having these variables separate will allow us to potentially have a very low health enemy that's

71
00:04:54,620 --> 00:04:57,870
very hard to hit and therefore be worth a lot more points.

72
00:04:57,890 --> 00:05:03,620
So with our variables all set, let's head down to our take damage method, which is where our things

73
00:05:03,620 --> 00:05:05,500
with health are being killed off.

74
00:05:05,510 --> 00:05:10,670
And because we're going to eventually have a lot more stuff going on in this if statement, it would

75
00:05:10,670 --> 00:05:13,700
make sense to refactor this into a new method.

76
00:05:13,700 --> 00:05:16,520
So let's call this method die for now.

77
00:05:16,550 --> 00:05:18,920
A little bit morbid, but it does the job.

78
00:05:18,920 --> 00:05:23,960
And then we can create our die method where we can destroy our game object.

79
00:05:24,050 --> 00:05:30,200
Before we destroy it though, let's add our score to our scorekeeper so we can say that if this is not

80
00:05:30,200 --> 00:05:38,000
the player, then we want to take our scorekeeper and modify the score passing in the score for this

81
00:05:38,000 --> 00:05:38,720
object.

82
00:05:39,610 --> 00:05:44,830
And before we go and test this in unity, let's jump back to our scorekeeper very quickly, because

83
00:05:44,830 --> 00:05:50,350
at the moment, until we've got our UI in, we have no way of checking with this scoring system is working.

84
00:05:50,350 --> 00:05:57,460
So when we modify our score, let's just add a nice debug log to tell us our score.

85
00:05:57,850 --> 00:06:01,150
So let's save up our scripts and jump into unity to test.

86
00:06:01,150 --> 00:06:06,400
Now let's head over to our prefabs so we can look at our enemy and our player.

87
00:06:06,520 --> 00:06:12,490
If we look at our player first and head down to its health script, we want to say that it is the player

88
00:06:12,490 --> 00:06:14,680
so it won't recognize any score.

89
00:06:14,710 --> 00:06:19,900
And because the score is mostly irrelevant, let's just set that to zero just in case we forget to check

90
00:06:19,900 --> 00:06:21,310
our is player flag.

91
00:06:21,640 --> 00:06:27,160
Then over on our enemy we should have our is player flag set to false and we want to give it some sort

92
00:06:27,160 --> 00:06:29,800
of score and I'm going to leave it as 54 now.

93
00:06:30,220 --> 00:06:36,520
So if we click on our console so we can see our score being logged out here and go ahead and hit play.

94
00:06:37,330 --> 00:06:41,890
And now if we try and kill some enemies, we will get our score being locked out.

95
00:06:42,580 --> 00:06:45,250
So congratulations on creating that scorekeeper.

96
00:06:45,250 --> 00:06:49,300
And if you manage to do it all from the challenge and give yourself a big pat on the back because that

97
00:06:49,300 --> 00:06:52,600
wasn't a small piece of work with all of that set up.

98
00:06:52,600 --> 00:06:55,840
Now we're in a very good place to start adding our UI.

99
00:06:55,840 --> 00:06:58,570
We'll do that in the next lecture, so I'll see you there.

