If we have a look at the UI of fruit ninja, we can see that we have a well, a score, a best score,
and then we have a timer that runs up.
And that's what we need to add to our game as well, of course.
So let's go ahead and create a new object first and call it game manager.
And in here we add a script.
So let's create a new script and call that, and we assign a new script to that game manager.
So let's create a new script here in our scripts folder and call that game manager.
Now let's drag the game manager into our game manager script.
Now let's direct the game manager script to our game manager and edit that.
Now what our game manager will do for now is count the score.
So let's create a method and several variables for that.
First of all, we will need an integer which will have the score value assigned to it, and then we
will need a text object.
And in order to work with text we need to add unity engine start UI.
So the namespace, the unity engine, the UI.
Now we can access the text, class, public text and we're going to call that score text and let's create
a method which will simply increase our score.
So public void, increase score, and now let's increase the score by, let's say two each time that
somebody calls this method or any class calls this method.
So increase by two.
And then also we want to set the score text.
So each time the score is increased, we want to update the text variable, of course, as well.
So score to string.
All right, now let's save that script and let's go back to Unity in here.
We will create a new object and I'm going to call that UI.
So let's change the name of the game object.
Let's call it UI.
And now let's add a UI text here that will create the canvas and I'm going to drag the canvas into the
UI game object.
And what I want to use here is that our canvas scalar doesn't have a constant pixel size, but that
it scales with the screen size.
And we can set the resolution to, for example, 800 times 800 or 19, 20 times 1080, whatever you
want to have.
So for now, we will just have the text in there.
So let's a look what this looks like currently and we don't see the text even so let's manage that.
Let's go to our canvas.
There you are.
And you see that our text is currently not even inside of our game.
Let's see, our UI element should be reset and now we can change the position of our text.
I want my score text to be at the top left side of my UI.
So let's zoom in a little bit and let's change the rect transform to the top left.
That will be the anchor preset.
So that means from this perspective on the values of X and Y are calculated.
So let's just choose zero zero.
And you can see that our text now is at this top left corner.
Now, good values would be, for example, 100 and -75.
And maybe let's change the size of our text as well.
So this will be the score text.
So let's maybe enter a value like zero in here and change the name of it.
So I'm going to call it score text.
Now, of course, the font size is a little bit too small, so let's change that.
Let's go with, let's say 120.
And as you can see now, it disappears.
So we don't see the number anymore.
And that has to do with the overflow.
So if you add horizontal and vertical overflow, what that will do is it will show the number no matter
what.
Now, no matter what the size here, for example, for the width and height is for this object.
So that's how it will currently look like.
Maybe it's not the perfect position to play around with it.
Maybe so let's drag it up there.
So the wide position of zero, I think that should be around.
Fine.
Now let's change the color of the text.
Let's make it an orange ish or orange ish color.
Like that.
Now let's see.
There is zero.
Of course, our background doesn't look as nice anymore.
We need to change the background of our game.
But at least we have our score text.
So now let's duplicate that text and call this one high score text.
And now let's change that high score text.
Let's drag it to the bottom and let's enter best colon zero.
And as you can see, this is, of course, way too big.
So let's scale that down to maybe 36.
That looks much better.
So look in the game best zero.
Okay, for now, let's maybe change the background of our main camera.
So now it's.
This color, this orange brownish color, and maybe this will look better now.
It's fine.
I think that's better for now.
Now, in order to make our number look a little bit cooler, what we can do is add an outline so you
can go here to UI.
So add component, then UI effects and then you'll find outline here that will do is it will add an
outline to your game so you could or to you to your text and you could do that like this.
As you can see now you can create an edge, an outline edge to your score text, your high score text.
Here, the white one would make sense if the main camera, for example, would be a bright color, then
a black one would look better.
So if it's like this color, for example, then maybe the score should have an effect.
Color of black.
So I think that looks much better than the high score text should get one as well.
So let's add in UI effects outline.
And as you can see now, they look much better.
All right, so now let's implement our score text in our code.
So our game manager already expects a score text so we can drag that in there and our current score
will be zero.
So if we set that to five, for example, let's have a look what happens.
Actually, let's set that to five endgame.
As you can see, nothing happens yet.
So we will need to add score.
So which means we will need to call this method here, this increased score method in our fruit script.
So whenever we create a sliced fruit, that means we had a we got an additional score.
So in order to do that, let's add find object of type
game manager and increase the score.
So this will now increase the score every time that we slice a fruit.
All right.
So now let's test it.
Let's start the game.
And let's slice through our fruits.
And as you can see, our score is increased by two every time.
And maybe we want to have different values based on the fruit that we have sliced currently only slice
oranges, but maybe we want to slice other objects as well.
Then we would also want to have different values, for example.
So currently let's just assign three here.
Now of course, we will get an error because the game manager itself doesn't know what these three should
do.
So let's add an int points in here and now instead of saying score plus two plus equal to, we just
increase it by whatever points is delivered.
So in our case now it will be three.
So every time we run or we slice a fruit, we will get three points now.
So let's check that out real quick, if that's true.
So.
Let's slice a fruit.
And there we are.
We get three points.
All right.
Now, next, we need to add a functionality that stops the game because now we can play the game endlessly
and will constantly get points and there is no punishment for any behavior.
And of course, we want to change that in fruit ninja.
That's usually done by the bomb or by the timer.
So if the timer runs up, the game will be stopped.
Or if we hit a bomb, then the timer stops as well.
So let's add the bomb to our game.
So let's go to the scene view.
Let's open our models folder.
And here we had the bomb.
All right, so now let's go into a scene view and let's drag the bomb into our game.
Well, actually, it's too big, so let's get rid of it again.
So let's delete the bomb and go to our bomb model.
Now let's change the scale factor to 0.3 and apply that.
And now we can drag the bomb in.
And I think that size is much better.
So now let's add some components to it.
For example, a physics 2D, rigid body and a physics to DX box or actually circle collider.
Here, this should be fine.
All right, the circle collider fits well, I think.
All right, so what do we want to happen if we hit a bump?
Well, we want to pause the game and show the game over screen or something like that.
So let's add a script to our bomb.
So I'm going to add a new script and I'm going to call that one bomb script or actually just bomb.
And now let's open that bomb script up in here.
We will add an on trigger enter to DX method.
So whenever we hit the bomb, we want to see if the blade hit us.
So blade b equals collision
dot.
Actually, it's not this collision, it's that collision here collision dot get component and blade.
So what that means, well, we've seen that code already, but what that means if the object that was
triggered or hit triggered us, if that's the blade, then do something.
And if it's not the blade, then return.
So if not B return, but if it is B, then we want to find object of type game manager.
So then we want to access our game manager and call a method from him.
So let's say on bomb hit.
So whenever we hit the bump now it complains that there is no method called on bomb hit and we'll find
that method in our game manager or we need to create that method and the game manager.
So let's create that method void on bomb hit and that should be public so that we can access it from
outside.
Of course, for now, let's just say bump was hit.
So debug log bomb hit.
So let's save that script.
Let's save the bomb script as well.
Now we see the error is not there anymore.
Now let's test that.
So let's go back.
Actually, let's drag the bomb script into our script folder while we're added.
And now let's run the game and let's see if I hit a bomb.
Actually, the bomb was moving too fast, so I'm going to push it to the top so I can actually what
I can do is for now, deactivate the.
Gravity.
So let's say gravity scale zero.
And let's see what happens.
And I can drag or hit the bomb.
Actually, nothing will happen, of course, as our trigger as there is no trigger in the circle collider.
Now let's see once again what happens now.
And as you can see, Bob hit is called Every Time that I Go Through the bomb.
All right.
So now we can change the gravity scale to one again.
That was just for testing purposes.
And now our bomb should work.
Now, what we want to do is we want to.
Stop the game whenever that happens.
So we can just say time dot time scale is equal to zero.
That will just stop the game.
So this time, the time scale is in general a functionality that allows you to reduce the speed of your
game.
So zero means there is no speed at all, which means there is no movement and you stop the game.
And one would be you have the normal speed.
So timescale one is the default value and timescale zero is, for example, to stop the game.
So let's save that script and check that out again and let's see if I can hit the bomb quickly enough.
In order to stop the game.
And as you can see, I dashed through it and my bombs were actually my fruits stopped and the bomb stopped
as well.
All right, now we only have one bump.
So this one bump is fine, but we want to randomly spawn bombs as well.
And in order to do that, we need to go back to our sponsor game object.
So our script better set.
So let's go to the sponsor script.
And currently we only have one fruit to spawn, so we can only spawn one type of fruit.
In this case, it's an orange, but what we want to have is to be able to spawn oranges, spawn bananas,
spawn whatever we think of, and also spawn bombs.
And in order to do that, we need to change this fruit to spawn to an array, which is an array of game
objects.
So let's add the square brackets and let's change that to objects to spawn, because it's not just fruits,
it's objects in general.
Now, of course, here we get an error because we try to spawn one simple fruit and this method is not
available anymore.
So what we need to do is we need to randomly create a game object, which is either a bump or which
is another fruit.
And then we simply say, okay, spawn that.
So let's go ahead and do that.
Let's create a game object, let's call the G0 and set that to NULL and then we have a random value.
So let's create the random value range.
We want to randomly create different kind of fruits or different kind of game objects.
For example, the bomb should spawn less often than something else.
So what I'm going to say here, if P is lower than ten, because the value can be between zero and 100.
So if it's lower than ten, then I want to spawn a bump.
So the chance of the bump is roughly 10% or the chance of a bond bump to spawn.
So I'm just going to say G oh is objects to spawn at the position of zero.
So now of course I'll have to take care of this.
So I need to assign the bump at the position of zero and otherwise so else I want to set go with objects
to spawn with a random range between one and objects to spawn length.
So that will do is with the chance of 10%.
I'm going to create a bump.
And with a chance of 90%, I'm going to create a different kind of fruit.
And here, of course, I need to use curly brackets because this is an index that we are trying to assign
this to.
So, once again, 10% chance, it's a bump and 90% chance, it's something random out of all of the
other objects to spawn that we have assigned.
And now, of course, we don't instantiate a fruit to spawn anymore.
We instantiate that game object.
Now, of course, you could you could create an object to spawn what you call fruits to spawn.
And then you could, of course, say public game object bump, and then you could just assign the bump
in here and you could now change gold to be Bob here.
That would be an option.
Of course, your range would be from zero to somewhere.
That would be another option.
Maybe that's even a slightly more intuitive option because the bump is a very specific kind of fruit
and whatever else we add, we can add as we like.
Because the other way that I was just showing you, we would always have to be careful that we assign
the bump to the very first spot.
And we know that because we have created the code.
But if somebody else starts to run the code or starts to edit the code, he might be confused about
that.
All right.
So now we make sure, okay, it's going to be the bob here.
Now, let's save that
and let's go back to Unity.
There you will see what's happening.
So our sponsor should get fruit to spawn.
Currently, it's just the orange.
And after a couple of seconds, it should change from fruit spawn to objects to spawn and bump.
All right.
So there's the bump and the objects to spawn.
And I'm just going to say, currently, it's going to be one object that I want to spawn.
And now let's go to our prefabs and let's drag the orange in there.
So spawn direct the orange in here.
And now about the bomb.
Actually, we don't have a bomb prefab yet.
So let's create a bomb prefab.
Let's drag the bomb into our prefabs folder.
And now let's drag that bomb into our spotter.
In order to test that, maybe we should set the value to 50 here so that there is a 5050 chance that
there is a bump that comes up.
So just to see whether there are bumps or the bumps come in general.
All right.
So there's a bump, oranges, bombs, oranges, bombs.
And if I hit an or a bump, well, I hit it outside, but I hit the bump and now, boom, the game stops.
So now let's see if we well, let's set that back to ten.
That's better.
And now the chance that a bomb comes up will only be 10%.
Of course.
And as you see, oranges, oranges, oranges.
And then we get points all the time.
At one point, a bomb might come up.
So far, none was there.
Actually.
Come on.
Where's the bomb?
As you can see, that's the thing with chances, right?
Chances are just chance.
So it could happen that I get a gazillion oranges here before I get a bump.
Well, the probability is very low and there is a bomb.
Perfect.
All right, so it took quite a bit.
As you can see, I already killed 40 oranges before I got one bomb, which means there's 10%.
Well, it's 10%.
It could happen any time.
And it only happened after almost 40 or maybe even even 50, I think even 50 oranges, because I didn't
hit every single one of them.
All right.
So I'd say let's go to the next video where we will see how to get the game over and to restart into
our game.
So see you there.