In this video, we are going to create a level manager script, the script will keep tracks all of the
achievement on the current session of the level.
So the script will come, the enemy kill also the number of human rescue.
So at the end of the gameplay, the script will be able to evaluate those values and unlock the achievement
accordingly.
Now let's open the scripts folder inside the folder.
Let's create a new folder.
And we are going to call this that the Scripps.
Inside the new folder, let's create a new script and call it level manager.
Now, let's open a squib.
First, I'm going to declare a static variable to be used as a static reference to make this script
easier to be accessed from under script.
And since there will be only one instance of the script in the scene.
This makes the script suitable for using a static reference and lets market static and for a time,
let's type level manager.
And let's just call it instance.
And let's initialize it inside unsweet, so I'm going to declare the wait method.
And we can initialize it by passing this keyword to the instance variable.
This will make sure this group occupied a static reference so other scripts can have access to the script
right away.
Next, I'm going to create a custom serializable class to hold or condense the achievement letter.
So let's set up here outside the level of your class and let's call it Medal's.
We will also need to add a serializable attribute so the values of this class can be serialized and
shown in the inspector and we can add it by accessing the system namespace.
Since this class is just for holding matter, we don't need to inherit from moral behavior.
So let's declare a couple of public boolean variable.
For the first one, I'm going to call it rescue.
And then kill, and the last one will be untouched.
And now we can instantiate this class as a variable inside the level manager script.
So let's declare it here with a public school and let's just call it Medal's with lowercase M and to
initialize it we can type equal new metal keyword and then open and close parentheses.
Now, if we save this script and head back to unity, let's create a new empty game object, reset its
transform and I'm going to reorder the hierarchy.
So this new game object stacked below the pulling manager.
Let's also rename the game object into level manager.
And next, let's attach the level manager scripts to it, once it's a patch, we will be able to see
the three booleans from the middle class field on the inspector.
And this is possible because we have declared the booleans inside the middle class.
So whenever the class are being declared as a variable on others scrip, we will be able to see it in
the inspector because the serializable attribute.
Next, let's delete the star and the update method, and then we will also need to declare a couple
of integer variables.
I'm going to call it total enemy enemy kill.
Total rescue.
And Human Rescue.
And next, we need to create a new public method with a return of void.
And let's just call it register Anami.
Another one method for registering the human rescue.
Another one called Enemy Kill for Counting the Enemy Kill.
And also another one called at Rescue for Counting the Human Rescue.
Inside the register, enemy method, we want to increment the total enemy variable, and I'm going to
also change the names of the variables to start with a lowercase letter for the sake of convention.
And for the registered rescue method, we need to increment the total rescue variable.
For both at kill and at rescue, we need to add our Inkerman, both of the enemy kill and the human
rescue variables respectively, and with all the variables calculated throughout the gameplay, we can
compare it at the end of the level and unlock the related achievement based on that comparison.
And then we need to also create a new public method.
I'm going to call it game end, and before defining this method, we need to create a routine first.
So let's create one by typing enumerator and call it count delay.
And in the beginning of the quarantine, I'm going to delay the court using yield return new wait four
seconds and delayed by zero point twenty five seconds.
After the delay, let's first compare if the enemy killed, value is equal to the total enemy value
and if it is.
Then we want to set the kill boolean ornamentals variable to true.
And next, we want to check if the human rescue is also equal to the total rescue, and if it is, then
we want to set the value of the rescue bullion on the medals were able to through.
And we need to create another public method for detecting whenever the player got hit, and we can call
it player hit.
Inside the method, we need to set the untouchable and values from the medals variable to false.
So in order to unlock this achievement, the player should not got hit throughout the level.
For untouched, we need to set its value to true on the beginning of the level so we can just set its
value inside the awake method so the default value will be true.
But once the player got hit, it will become false right away.
And next, we want to run the company like routine from inside the game and method, so let's start
colluding to execute a routine and pass the codename and also a set of parentheses.
Another thing I want to add is a unity event, but before using it, we need to import the unity engine,
that event namespace here above.
So let's just do that and let's declare a public variable with a type of unity even.
And let's just call it on game, and we want to invoke this unity event whenever the game end.
So let's add it inside the routine by calling the invoke method.
This way, we can add custom actions from other classes or objects that we want to invoke via the inspector,
similar to the event under the button component.
So now let's save Descript.
Next, we need to modify the health system, basically, we need to register an enemy whenever they
start method gets in bulk.
So let us call the registered enemy method from the level manager script by accessing its static reference
like so.
Hi, this is Romi from the Future.
It seems as of currently, this court will cause an issue because the health system is being used by
both player and enemies.
The total number of enemies on the scene will always be the real number of enemies, plus one, since
the player will also register as an enemy on start.
And we can easily fix this because we have declared a billion to mark whether the health system is attached
to an enemy or not.
So using this boolean, we can add an IV check before running to register enemy method.
So let's add if open parentheses is enemy and close parentheses, then we want to run the register enemy
method.
Next, inside the tech health method, whenever the enemy is defeated, we want to call the enemy kill
method from the left, eliminate your script to record the enemy kill value.
So that's modified if its enemy is through blog and call the method inside this if stateman.
This way, the enemy killed variable.
And the total enemy will have a certain values at the end of the level.
So when calculating the result, we can unlock the achievement by comparing both of this value and we
want to register the total amount human that needs to be rescued and also the amount of human that has
been rescued.
So let's open the human rescue script and we want to trigger the register rescue method on the start
method and call the rescue method whenever the human gets rescued by the player.
And it seems we need to define the start method here, so let's just create the method and we can call
the register rescue method by accessing the level manager that instance and then call the method.
And once the rescue time is finished here inside the routine, we can call the rescue method.
So let's add the method and we can add it above the rescue, not invoke line this way.
We can also compare the human rescue with the total human on the scene at the end of the level to unlock
the human rescue achievement.
I have noticed about during testing where the timer continues even if the player gets destroyed.
So we need to check if the player has been destroyed or not.
We can do that by creating a private variable type of game object for grabbing the player game object.
And inside the start method, let's check if there is any game object with the tag of player, then
we want to grab that game object and cash it into the player variable.
And next, inside the rescue, quarantine inside the wild stateman, when the time is still greater
than zero, we want to check if the player is not ANA.
And if it is, then we want to stop recruiting.
And set the timer you, I feel, amount to zero.
This way, as soon the player gets this right, it will stop the timer and clear the timer, Ewa.
Another thing that we need to set up is for the untouched achievement, so going back to the health
system inside the trigger enter method whenever the script owner get hit by a bullet.
We want to add a check.
If this is a player by checking if there is enemy value is false using the shorthand version, then
we can call the player hit method from the level manager.
This will disabled and touch achievement assoon when the player get hit by a bullet.
Let's save the script and let's head back to unity.
And if we select the level manager game object, you see we have a couple new integer fields.
And as well as a new on game and unity even.
And let's test this again, as soon as we enter the play mode, you see the total enemy and the total
rescue values change depending on the amount of the enemies and human rescue on the scene.
It seems my plan gets destroyed.
So I'm going to replay this again.
Now we have killed one enemy.
And I missed killing all of this enemy, but it's OK.
I've destroyed another enemy, and if we inspect the inspector, we have total seven enemies and we
have killed five enemies so far and we need to call the game and method inside the manuscript whenever
we kill the boss.
And let's try to rescue the human.
And once we have rescued the human.
The human rescue variable increase.
And let's let's take a couple of things before we finish this up.
First, we need to make sure to run the game and method from the level manager script whenever we have
killed the bus.
And to do this, let's select the bus ship object on the scene under the bus activator.
If you remember, the bus object consists of a couple of parts, and on the main game object, we can
see the collider is disabled when it first appeared because we need to destroy the threats first.
So when we destroy the first threat, it will activate the second threat collider.
So we can destroy it and after it gets destroyed, it will activate the main body collider.
So to run the game and method, we can add a new entry on the death, even field under the death system
component of the boss ship game object.
Next, let's drag the Lefler manager game up into the new entry object and then let's run the method
by going to the level manager component on the dropdown and select game.
And this will trigger the method whenever the boss ship is defeated.
And now before we test this.
Let's select the player game object.
And enabled the shooting script, the default bullet, and don't forget to highlight the level manager
so we can see the value changes for the achievement.
And as of now, there is a bug where the enemy killed is currently much bigger than the total enemy,
but it's OK.
We will take a look at it in a moment.
And let's fast forward to the buzz.
Now, we have killed the bus, but because of this bug and we didn't achieve the other medals, none
of them gets activated.
So let's fix this.
So in the level manager script, instead comparing the value with an equal compare, let's change it
to a greater or equal just to be on the safe side.
Back to unity, let's give it one more try and I'm going to fast forward the video and for the enemy
killed value bug, we need to add attack if the enemy is already been destroyed.
I need to also readjust the human rescue position so we can rescue it before engaging the bus.
Now, we have defeated the bus and as you can see, we have unlocked the killer achievement, I'm going
to pass the game.
Then highlight and focus to the player, and as you can see, the plane got hit somewhere in the level.
And if we check the level manager, we have failed to get the Antje achievement because of this, not
a thing that we need to do is to also invoke the level manager game and method when the paradise.
So if we select the player, let's add a new entry on the UNDEF event field under the death system and
the level manager game object to the object slot and under the drop down menu, select level manager
and select the game and method just like what we did with the boss ship.
This way, whenever the player win or lose the level, the game end gets invoked and the player can
go back to the menu or a level selection screen to fix the box issue where the enemy killed.
Value gets much bigger than the total enemy.
We need to make sure that the enemy kill method only gets executed once when the enemy killed.
So inside the health system script, we need to add a boolean variable that will act as a safeguard
for this issue.
Let's just call it that.
And inside the tech help method, under the if is enemy block, let's add a second condition for the
if statement with an operator and check for if the debt boolean value is false.
So the code inside this, if will only gets executed if both of the conditions are true.
And inside, let's start with that boolean to true.
This will prevent the bullet collision to trigger this part of the code and thus will make sure the
enemy kill only kalt once.