Framework: HUD Control.
Objective: Create HUD Control.
In the First step, I will create a canvas with Score, AI Count, and TimeRemaining Text, and add UIManager which is a singleton.
I will Create a Text variable in UIManager, called score text which I will assign the string at the start.
Now Create a public method with the int parameter, then assign the text which will be the score plus the amount parameter.
Go to the player script and create a variable called addPointForKillingEnemy
Now call this method Inside the player script in addPointToPlayer.
Go to the Pooling Manager script, then create an int variable called aiSpawnedCount and make it public.
Go to the requestedPooledObject method when we check if objects are not active, then we need to add another check to see if aiSpawnedCount is greater than zero.
If aiSpawnedCount is greater than 0, we decrement by one then we need to add logic to check if aiSpawnedCount is zero we set it to zero.
Then we need to add another logic we don't need to add another enemy if all enemies are active so here I will add another if condition to check if amountofAI is less than the Aiobjects array.
Note: This will make sure we get no other AI even if all enemies are deactivated either by killing or reaching the destination, this makes sure we only have limited ai which defines by the amount.
Now go to SpawnAIRoutine in the SpawnManager script, here we need to avoid getting a null reference because in RequestedPooledAI we won't be able to return the enemy if aiSpawned is less than zero. But Spawn Routine will call this method again we need to avoid that.
To solve this we need to spawn another enemy when if the enemy count is greater than zero otherwise we set aiSpawned to true when we activate the last enemy, in the next frame _isspawned will be to true so we won't be able to call SpawnAIRoutine.
Go to the UI script create enemy count text, then initialize to the amount of ai you want. Then create another public method called UpdateEnemyCount with an int parameter called amount, last append enemyCount text dot text with the amount.
Go to Pool Manager and create an int variable called enemyRemaining then Initialize enemyRemaining equal to the amount of Ai when the game starts/on the Start method. Also, create a public method called EnemyCount which will decrement the enemy remaining by one in the same method we will call UpdateEnemyCount and pass the enemy remaining as a parameter.
Now go to the AI script and Call EnemyCount when AI health is zero.
Now it's time to add a countdown, I won't explain this because this needs to be an article by itself.
Here is an article I use to Implement this
This is how I implement Hud control in my game.
see you in the next one.