Okay.
Ready for some more?
I still receive the message Enemies remaining :1 throughout the whole game play even after applying your instructions.
Isnât that because there is an enemy in front of you in that scene?
if I shoot the enemy it still states remaining:1
Chances are you still have a GameObject in the scene with the enemies tag that doesnât need it then.
That is what I thinking to I looking for it. Hold on
When I checked at this end, you had tagged the GameController and the Enemies GameObjects - both need to be set to Untagged
I found the issue but the game says all gone all the time it was I had the Enemies tag on the the parent
Enemies
Yeah, thatâs what I said above.
Ok - so all sorted again now?
Did you want it to say all gone all the time?
It should only be saying that when the last enemy is destroyed.
I will try again.
Just to check, in that screenshot above, is that at the beginning of the game, or, have you circled all the way around and effectively started again?
Also, when I said âEnemiesâ above, I was referring the GameObject called âEnemiesâ, I didnât mean changing the tag of the actual, individual, enemies - those should still be set to âEnemiesâ - if that makes any sense.
The begining
Yes this is what I did.
So it should be like this (excuse awful diagram), Iâm rushing here a bitâŚ
Okay got it. Hold on.
Same results
Great, ok, lets do the next bitâŚ
Performance tweakâŚ
This is bad -
void Update()
{
GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemies");
enemiesLeft = enemies.Length;
if (enemiesLeft == 0)
{
endGame();
}
}
Currently, every single frame you search the entire scene and try to find GameObjects that are tagged âEnemiesâ. All of the âFindâ methods are slow and should be used sparingly, certainly not in an Update statement. Another problematic issue with this approach is, and you may have already spotted it, but when the game runs, your count gradually increases as you fly around the scene. The reason for this is because at the start of the game the Timeline is disabling all of the GameObjects that are not required at that time. You count is running each frame and just returns a count of what is âactiveâ at that time in the scene, not all of the enemies that the player needs to shoot in the level.
If you add this one line to your Start
method within GameController.cs;
Debug.Log("Enemy Count : " + GameObject.FindObjectsOfType<Enemy>().Length);
At the beginning of the game youâll get the total of all enemies that are in the scene. Whilst it is still using a âFindâ method, it is now only occurring once, so hugely better from a performance perspective. The issue about only returning the number of âactiveâ GameObjects is still true, however now you are capturing the count before Timeline disables any GameObjects. Now, this could be by chance, as the Timelineâs Playerable Director is set to âPlay on Awakeâ, we could move this line into an Awake
method but again, the Timeline could be run first, to be certain we would need to consider changing the script execution order to ensure that this count is obtained before the Timeline disables anything.
For now Iâd not worry about that, if you get the result you need, then maybe just make a note in a comment about the script execution order in case its something which bites you later.
Now, as we can output the count using the Debug.Log
statement, we can also just set the variable here, once, so;
void Start()
{
enemiesLeft = GameObject.FindObjectsOfType<Enemy>().Length;
restartText.text = "";
gameOverText.text = "";
youWinText.text = "";
youLoseText.text = "";
score = 0;
UpdateScore();
StartCoroutine(SpawnWaves());
}
and then you can update the Update
method to no longer perform this âFindâ;
void Update()
{
if (enemiesLeft == 0)
{
endGame();
}
}
Run the game, and youâll find that it states 13 enemies at the top left, and, as you shoot them it wonât currently decrease, this is ok, get to this point and then let me know