- Home /
How to stop enemies dying on spawn
I am relatively new to unity and C#. I am trying to make a simple dungeon crawling game. Everything is working fine, apart from the code for my enemies health. the if statement telling the game to destroy the enemy game object makes them die as soon as the game is played. An identical piece of code is used for the player and this works fine. any help would be greatly appreciated. code is below, the part that isn't working is the comment at the end. Thanks in advance!
public class StrEnemyHealth : MonoBehaviour {
int StrEnHealth = 20;
void OnTriggerEnter(Collider collision)
{
//takes health from enemy
if (collision.gameObject.tag == "Player")
{
StrEnHealth-=PlayerStats.Att;
Debug.Log("enemy health:" +StrEnHealth);
}
}
/*
//kills enemy if health is 0
void Update()
{
if (StrEnHealth <= 0);
{
Debug.Log("enemy dead!");
Destroy(gameObject);
}
}
*/
}
I thought nothing else could access this variable because it is not public? i have looked through the code for everything else, and nothing is accessed again.. I have also double checked all tags and everything is correct.. I really am at a loss!
Answer by ThePunisher · May 14, 2014 at 06:11 PM
Edit:
Found the problem. The if statement has a semicolon appended to the end of it, and it shouldn't. Change it to the following code:
if (StrEnHealth <= 0)
{
Debug.Log("enemy dead!");
Destroy(gameObject);
}
What's happening when the semicolon is in there is the if statement evaluates but does absolutely nothing. Then the code in the curly braces will always execute regardless of the evaluation result of the if statement.
Hi, yeah i'd noticed this seconds after i posted! thanks for the help but still hasnt fixed the problem
What is occurring now? Are there any other scripts on the enemy game objects? Or perhaps the enemies are tagged as "Player" and are colliding with each other?
the same is happening.. on play, the enemies die before the game even starts, the debug console just lists all enemies have died. there is an AI script that controls movement, an asset i have found online. if i leave that end portion of code commented out, everything works perfectly, apart from the enemies not dying.
Well, we know two things so far.
1) The enemies only die if the update code is uncommented. This means that nothing else is accidentally killing your enemies (its a good thing).
2) The only way for your enemies to die if the update code is uncommented is if the enemy's health reaches a value that is less than or equal to 0.
One thing to do is check anything that modifies that health value, especially outside of that script. If nothing outside of this script modifies the health value, then we know one more thing. The only way for the health value to be reduced is when a game object tagged as "Player" collides with the collider of the object this script is on. That means that your enemy's must be somehow colliding with your player or something tagged as "Player" upon starting. Ensure nothing else is accidentally tagged as player.
I think I found the problem, check the updated answer.
Your answer
Follow this Question
Related Questions
Instantiation array of enemies, c# ? 2 Answers
I can't have more enemies at the same time. 1 Answer
Arrays of Enemies in a Game HELP! 0 Answers
Changing Mesh of an object depending on Health Value. C# 2 Answers