Placeholder Image

Subtitles section Play video

  • So far we have a game

  • where we can shoot a singular enemy

  • and you can be killed by

  • that very same enemy.

  • But currently

  • there is no way to score points.

  • So we want to add more to our UI

  • and we want to add the ability to

  • score points and represent that inside

  • our UI as well.

  • Okay, so, what I want you guys to do

  • is to take a look back at your hierarchy

  • and we are going to look at the HUD Canvas.

  • So the HUD Canvas is our

  • UI canvas, as you remember earlier we placed

  • in the Health UI which gives us the slider for our health

  • and the little heart icon

  • as well as the damage image.

  • But this time we are going to create a score text.

  • So what I'm going to do very quickly is just a the top

  • of the scene view click the 2D button

  • to switch back to 2D mode and I'm just

  • going to zoom right out

  • or what I can do is double click my HUD Canvas

  • to frame it and then zoom back in.

  • What I'm doing is selecting my

  • rect tool because whenever I work on

  • UI stuff I want that 5th tool,.

  • Show us the 2D button really quick while you're zoomed in.

  • There it is, 2D button.

  • Once we're in 2D mode we can then

  • go ahead and create some more UI

  • so this time we're going to make a

  • child object of the HUD Canvas.

  • Therefore I'm going to right click it

  • go to UI and Text.

  • So these UI things are basically a

  • collection of ready-made objects that

  • you can start working with.

  • All of the things that are in the UI system,

  • much like the rest of Unity,

  • are actually components, so what we're really doing

  • is creating a new game object

  • with a text component attached to it.

  • By default when you make a new text component

  • you made something that is default Arial text

  • and it is in grey so that it will

  • work neutrally on light or dark backgrounds.

  • So we're going to rename this

  • The first thing we're going to do is call Text

  • ScoreText, so capital S and T.

  • So rename Text to ScoreText.

  • Then what I'm going to do is to

  • re-anchor this to the top centre of the screen.

  • So if you remember we learnt about

  • rect transform's anchor presets.

  • And the way that we're going to do that is just

  • to set the anchor rather than all of it

  • to the top centre.

  • So it's this preset here.

  • We don't need to Alt, we don't need to Shift,

  • we just need to click that singularly.

  • And what that does is moves our anchors

  • to the top, so you can see our

  • little flower pattern thing is now

  • sat at the top.

  • And from there we can then adjust

  • the positions as appropriate.

  • You'll now noticed that because I've moved those anchors

  • the Y position is -220, so the centre of the game view

  • is -220 pixels or units from the top.

  • So now I can say the Y position is going to be -55

  • and I'll make sure that my X is also on 0.

  • That moves the text in relation to the anchor.

  • Yeah, so if I set that to 0

  • you can see that the pivot is 0.5, 0.5, in the centre.

  • But if I drag this down

  • you can see that I'm moving it a - value.

  • So I'll put that around -55.

  • The next thing we're going to do is setup the width,

  • I'll set that to 300.

  • And I'm going to set the heigh to 50.

  • And I'll set the color to white.

  • so in the text component you have all the controls

  • for how the text displays

  • and I'm going to drag in the color picker

  • so that my color for the text is white.

  • Then because we don't want it to just be Arial

  • and very small we're going to set the font.

  • And we're going to use the circle select and

  • choose LuckiestGuy, so that's a font

  • that we've included in this.

  • If you're not used to doing any kind of UI

  • work in Unity, because Unity is authoring another game

  • or application effectively

  • you need to include that font within your project.

  • So we have the truetype file for LuckiestGuy within that.

  • So we have the licence for it and we have the font itself.

  • That means that when we export it will have

  • the font and use it, it doesn't work like

  • word processors or Photoshop, it won't

  • just be able to pick from your library, you

  • have to create a copy of the truetype within your project.

  • So our score text has that font

  • and we're going to set the font size to 50.

  • And we're going to use the alignment under paragraph

  • to centre and middle.

  • So centre and middle and font size to 50.

  • And you should see that we have new text written in there.

  • Obviously we don't want it to say new text,

  • we want to see what our actual score will look like.

  • So in the Text field I'm going to type

  • in Score: 0.

  • That's the default that it's going to look like

  • when we start the game.

  • Also important to note that we don't have to

  • set the text to say Score: 0.

  • Our script is actually going to

  • write what it is that text should be

  • however it's really hard to tell what

  • this is going to look like when we're playing our

  • game without putting some value in there.

  • So you might say later 'why did we set that text

  • when the script is already doing it?'.

  • The reason is so that we can visually see

  • 'okay, that looks pretty good'

  • now let's go ahead and apply our scripts and do the rest.

  • So it's just a placeholder.

  • Now that we've done this I'm going to save my scene.

  • So File - Save.

  • And the next thing I'm going to do is put a

  • slight drop shadow, so there are some

  • effects that come with the UI system and

  • we can add them as a separate component.

  • We can keep the ScoreText selected,

  • go to Add Component and just type the word Shadow

  • and it will immediately find that component

  • and you can hit Return.

  • That will just give you a slight drop shadow.

  • I'm going to make it a bit more obvious

  • by changing the Effect Distance to 2, -2 in the X and Y axis.

  • It's also important to keep that

  • Use Graphic Alpha checked,

  • otherwise if you change the alpha

  • of the text the shadow won't also change.

  • What you'll notice about this is if I change the

  • alpha of the text itself the shadow

  • underneath is also fading out.

  • Whereas if it's not checked

  • we can fade this and then the shadow

  • will get left behind which is not desirable.

  • And then we need something to set

  • the score, something to be managing

  • the score, updating the text component's

  • text value with Score 10, Score 20,

  • whatever happens in the game.

  • And the way that we're going to do this is

  • by adding a Manager script.

  • So what I'd like you to do is look in

  • the Scripts - Managers folder

  • and you will find out Score Manager.

  • We're going to drag and drop this on to the

  • ScoreText game object

  • Then once you've applied it you should see

  • it at the bottom of the list of components

  • right underneath the shadow

  • and we can double click to open that up.

  • So at the start we again have our public variables.

  • You'll notice there's a new keyword there

  • Static.

  • So a static variable doesn't belong to the instance

  • of the class it belongs to the class itself.

  • So let me explain.

  • Whenever we're dragging on EnemyHealth

  • or PlayerHealth or PlayerMovement on to an object

  • we're creating an instance of that class

  • and applying it to the game object,

  • so they are all instances of a class.

  • And so all of the variables,

  • they're instance variables,

  • each enemy has it's own health,

  • each player has it's own speed, etcetera.

  • Static variables do not belong to an instance,

  • they belong to the class itself.

  • So what that means is,

  • in order to reference the score there

  • we don't need to go ScoreManager variable

  • GetComponent ScoreManager then use it, we just say

  • ScoreManager type . score.

  • So we don't need to create a variable to use it

  • we're just going to use it through the type itself.

  • So it only effectively exists in one place

  • we're not going to address a bunch of instances where

  • this exists, we're changing it in 1 place.

  • We could still have multiple

  • instance of ScoreManager,

  • we could drag multiple ones on to

  • a game object, on to different game objects, doesn't matter.

  • We're not going to, because that would break everything.

  • But if we did all of them would share the same score.

  • because it belongs to the type

  • not to the instance.

  • So the next thing is we need a reference to our Text component.

  • In awake we're going to setup that reference

  • to the text component.

  • Then we need to reset the score

  • because if we die we want

  • the game to reset, so,

  • we need to set the score back to 0.

  • And in our update function

  • what we're doing there, the text.text is

  • we're changing the text property

  • of the text component.

  • Okay, so the text component that we have

  • that string that we said Score: 0,

  • that was the Score text,

  • that was the text property of the component.

  • So what we're doing is we're setting that

  • to a completely new string, we're not changing that,

  • we're just setting it completely afresh.

  • We're changing it to Score:

  • and then that number will be the score.

  • So very simply that's our ScoreManager and

  • if you happen to save it it'll ask you to convert

  • the line endings, it's no big deal.

  • Okay, we'll need to continue scoring points

  • and I'm going to select my

  • Zombunny in the hierarchy

  • and locate the EnemyHealth script.

  • And we're going to open the EnemyHealth script.

  • and have a look down at the very bottom

  • at the StartSinging function.

  • James mentioned the public static

  • integer score earlier,

  • he promised you very kindly that you could

  • indeed say the name of

  • the class, ScoreManager.score,

  • so without saying GetComponent

  • or create an instance of the script, assign to this part of the script

  • we can very simply just say ScoreManager.score.

  • So we're going to re-enable that by deleting

  • the 2 // comments.

  • And what we're doing there is adding to it

  • the value of ScoreValue.

  • So scoreValue within this particular script is

  • a public variable that we can change.

  • This enemy has a value of 10 that when you kill it you get 10 points.

  • This way we can apply this

  • EnemyHealth script to different enemies

  • and have different score values.

  • So if you wanted to make the

  • killing the elephant worth a lot then you could

  • change that value, you don't need to go in to the

  • script and change it, it's a public value

  • so it appears in the inspector.

  • I just want to make a quick point about static variables.

  • So you know it's a lot easier to do it with static there,

  • we didn't have to create an instance variable,

  • we didn't have to assign it in awake

  • and then use it, we just used it just like that.

  • So why don't we use it for everything like that?

  • That would be so much easier?

  • It's because we have multiple enemies

  • and if we wanted multiple players then

  • we'd have more of those as well.

  • So if we wanted to change the health

  • of one player all of the player's health would change.

  • So we can't do it most of the time.

  • It's just very specific circumstances

  • where you'd only have one score

  • so we can make that static

  • to make it easier for ourselves.

  • So we're going to save this, it's going to ask you

  • to convert line endings, just choose Convert.

  • This project was made on PC and then

  • moved between PC and Mac so the files

  • get confused but it's no big deal.

  • So we should now go ahead and try it out.

  • So if you save your scene and press play

  • at the top of the interface.

  • There we go, 10 points for a Zombunny.

  • A very important point to make right now about prefabs

  • is that they are incredibly useful when you want to

  • spawn more than one object.

  • So some people might use if for rockets in a game

  • and you might use it for enemy spawning in a game.

  • You can use it for really anything you want to

  • But we want to use that for our enemies,

  • there's going to be 3 types of enemy,

  • and you guys have gone and created that first enemy.

  • What we don't want is just the enemy to be

  • sat next to the player when the game starts.

  • So what we need to do is to save him as a prefab.

  • So everybody make sure you've stopped play,

  • so play is no longer on, it should be black at the top.

  • No more blue buttons.

  • What we're going to do is to select our

  • Prefabs folder in the project

  • and then grab the Zombunny in the hierarchy

  • and drag and drop it in to the project panel

  • either in the empty space or drop it on

  • to the Prefabs folder.

  • Both of those will create the same effect,

  • you will get a Zombunny prefab,

  • which looks like this.

  • And you will have all of the same settings

  • that you had on the version that's in the scene.

  • So that version in the scene now belongs

  • to that prefab parent.

  • And even if we delete the version in

  • the scene, in the hierarchy,

  • then the version in the project is saved,

  • and that's very crucial.

  • Everybody check that you've got your Zombunny

  • in the project, it's very important.

  • Then in the hierarchy we want to get rid of it,

  • so I'm going to select it there and

  • on Mac Command Backspace,

  • on PC just the delete key.

  • Remove it from the scene.

  • And then save your scene.

  • Switch off 2D mode and double click the

  • player to zoom back in to the action

  • so you can see the player.

  • Okay, so that is the end of phase 8.

  • Subtitles by the Amara.org community

So far we have a game

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it