Okay
Have the same result as described above?
Great, thatās what we wantā¦ ok, next bitā¦
Before we start, delete the KillEnemy.cs script, it isnāt being referenced by anything and we have a few similarly named things in this project, it will be good to cut one loose
In order to reduce the count, we want the enemies to report āIām deadā to the GameController. Initially we can just use a method call to do this, but later on you could consider using events/delegates - letās keep it simple for the time being.
Soā¦ letās add a simple method to GameController.cs to help reduce the number of enemies;
public void DecreaseEnemyCount()
{
enemiesLeft--;
}
That gives us something to call, but we need to call it from something. As the enemy is being destroyed, it makes sense for the enemy to communicate back to the GameController.
You already have a method named KillEnemy
within the Enemy.cs script and this class already has a member variable storing a reference to the GameController, perfect, letās add a line of code to the existing method;
private void KillEnemy()
{
GameObject fx = Instantiate(deathFX, transform.position, Quaternion.identity);
fx.transform.parent = parent;
gameController.DecreaseEnemyCount();
Destroy(gameObject);
}
Run the game, you should now find that as you shoot the enemies, the count at the top reduces.
Yes it counts down but its not accurate the count misses two enemies and states all gone before the enemies have all died.
It is miss counting the last five ships and stating all gone to soon.
Thereās a reason for that, and I was just writing it up for you.
Bug fix
I have discovered whilst testing with the one enemy, that if I shoot it, I can get a remaining enemy count of -1, that would indicate that that our KillEnemy
method is being called more than once, and in doing so, it calls our GameControllerās DecreaseEnemyCount
method more than once for each enemy.
The problem here is the logic being used within the OnParticleCollision
method of Enemy.cs.
It only cares if the value is less than or equal to one, so if two particles hit in the same frame this message is sent twice and the code will run twice. We need to put a little fix in for that.
We could use a bool, perhaps an isDead
flag, and then set it, and test that in this logic, but we already have that. We have itās enabled/disabled state, so letās use what we already have;
Update the OnParticleCollision
method as follows;
void OnParticleCollision(GameObject other)
{
ProcessHit();
if (hits <= 1 && gameObject.activeSelf)
{
KillEnemy();
}
}
and then within the KillEnemy
method will set the GameObject to be disabled;
private void KillEnemy()
{
GameObject fx = Instantiate(deathFX, transform.position, Quaternion.identity);
fx.transform.parent = parent;
gameObject.SetActive(false);
gameController.DecreaseEnemyCount();
Destroy(gameObject);
}
So, when the first time the KillEnemy
method is called, it will disable the GameObject, the second time a particle makes the collision and the hits are less than or equal to one, the GameObject will no longer be active so the second part of the condition will fail and KillEnemy
will not be called a second time. At the end of the frame the GameObject is actually destroyed.
Ok, so thatās five hours of my time and about 40 posts between us. It is now official time for me to get some food
Thank you so much
Ok, the food is cooking.
So, what is left that needs to be done, from your perspective, not for the entire game, but just for these issues you were having?
You have so far;
- the count of all enemies in the scene
- the count reduces when they are destroyed by the player
- game over is triggered when the player crashes
Its okay we can finish up another time enjoy super. I will reach out to you next week I would like to try and tackle some on my own and get back to you where I get stuck. I Thank you for all you do, Hope itās Okay
No problem at all, go for it
Hi Rob Good Morning, I get the game to to sates the You Win! message after killing all the enemies, But I canāt get the game to to state at the end of the main timeline if enemiesleft to state the You Lose! message. it states both message when I killAllEnemies
if I remove the youLose statement the youWin statement still works. Thank you
void endGame()
{
killedAllEnemies = true;
}
void OnGUI()
{
if (killedAllEnemies)
{
youWinText.text = "You Win!";
youWin = true;
}
else if (enemiesLeft <= 1)
{
youLoseText.text = "You Lose!";
youLose = true;
}
These are the errors I receive
Should have kept going last night Martin, I had time available then. Unfortunately I wonāt be available until next week now. Happy to pick back up in due course.
No Worries Pal, just when you have time get back to me Thanks
Out of interest, how familiar with Git/GitHub are you?
Iām conscious each time I help I need to download your most recent version because youāll have made changes in between which involves download a whole load of things that havenāt changed and wastes time. If you are familiar with Hut and GitHub you could commit it all to a repo and then Iād only need to get the changes each time.
Just a thoughtā¦
I read into it over the weekend get back to you soon
Ok. It can be a bit bewildering at first. I would highly recommend using a GUI client instead of the command line, GitKraken is very good and is free.
I would also highly recommend practicing with a test/dummy project, not something you would be concerned if you lost. Just as a precaution.
Okay I just trying to get a hold of contacts at my school to discuss March session. I will work on this a little later.
Hi Rob, I Thank you GitKraken seems a lot more fun and easier to use. I wish Ben had use this from the start. I committed my files when your ready to look over it let me know how to share with you.