- Home /
When I kill one enemy, it kills them all.
Hello, I have been researching this problem for a while and I keep coming across people who need to change their variables to non-static. I've tried this and it still isn't working. I am trying to make a 2D game where a set number of enemies come into the room to try and steal the loot from the player, the player can move towards the enemies and click to make them take damage. Everything is working fine except when I kill one enemy all of them die and they die with one hit. I am using c# as I haven't used JavaScript very much. Any help is greatly appreciated. :)
public class EnemyScript : MonoBehaviour
{
private float curHealth = 40f;
private float maxHealth = 40f;
private float decHealth = 4f;
public static int EnemyCount = 0;
void Start()
{
EnemyCount++;
print("Enemy Count = " + EnemyCount);
}
void Update()
{
if (curHealth <= 0f)
{
destroyobject();
}
}
void destroyobject()
{
Destroy(this.gameObject);
}
void OnCollisionStay2D(Collision2D collision){
if (collision.gameObject.tag == "Player" && Input.GetMouseButtonDown(0))
{
curHealth -= decHealth;
LevelManager = GameObject.FindObjectOfType<LevelManager>();
LevelManager.EnemyDefeated();
}
}
In the LevelManager script function EnemyDefeated() it keeps track of how many enemy objects are left and when all the enemies are gone, it loads a win screen.
Answer by DennisWachtendorf · Jun 17, 2017 at 01:25 PM
Are you sure it kills all the players? Or maybe just start to show the winning screen? You call the LevelManager.EnemyDefeated method as soon as you kill one enemy, maybe that's the problem?
I'm pretty sure it kills all the enemies, the code for the EnemyDefeated function is below.
public void EnemyDefeated()
{
ChestFind.EnemyCount--;
print("EnemyCount " + ChestFind.EnemyCount);
if (ChestFind.EnemyCount <= 0)
LoadLevel("Win Screen");
}
I also forgot to add a couple of things to the code above. I have added them now. Thanks for the reply :)
facepalm
Okay thanks for that. You made me rethink this part of the code and I realised that I had asked it to say that it had defeated an enemy when it had only hurt it. I hadn't picked it up as it was also decreasing the enemycount which was what I was checking. I took the Level$$anonymous$$anager stuff out of here
if (collision.gameObject.tag == "Player" && Input.Get$$anonymous$$ouseButtonDown(0))
{
curHealth -= decHealth;
}
and put it here
if (curHealth <= 0f)
{
destroyobject();
Level$$anonymous$$anager = GameObject.FindObjectOfType<Level$$anonymous$$anager>();
Level$$anonymous$$anager.EnemyDefeated();
}
Thanks again for the help :D
Answer by Killerbro389 · Jun 17, 2017 at 04:22 PM
Aren't you missing a curly bracket at the end
Could be but that's probably just a copy-paste mistake. Curlies always come in pairs: if you have an extra one or are missing one, your code won't compile. There's no situation where you could cause a bug by adding a single curly somewhere in the code.
Oops, but yea Nose$$anonymous$$ills is right. It was just a copy paste error, my actual code has enough curly brackets.