- Home /
The question is answered, right answer was accepted
Math Doesnt Make Sense
The enemy spawns in with 100hp, the gun does 5hp of damage per shot. But whenever I shoot the enemy, it's health goes to a ridiculous number eg. -5568, and back to 100 instantly.
private float health = 1000;
private float weaponDamage = 5;
public void EnemyDamage() {
health -= weaponDamage;
print (health);
if (health <= 0) {
Die();
}
}
void Die() {
Destroy (gameObject);
}
I can't see the error there, haven't you another script that modify the health property? also, is the gameobject destroy imediately? Also, with the console, i see that between two prints, the health value get down of 13, so i think the script is modified somewhere else...
Alright, I'll go through the rest of my code and look for anything. Thanks :)
The script you post and the output you display dont match up. You got 2 scripts on the go?
I see Enemy hit which suggests you have an OnCollisionEnter somewhere interfering with it (as thats the kind of debug output people put there). Is that script also dealing damage?
I would recommend turning off the Collapse option in the console, it only gets in the way of understanding what's going on in these kind of situations. Personally, the only time I would have it turned on is when I'm specifically interested in the number of times a line is being hit.
Answer by DoctorSqueaks · Nov 03, 2015 at 12:50 PM
I've worked out what's happening, every time I spawn the prefab, the health doesn't reset back to 100, which is why, after a couple hours of work, I was getting ridiculous numbers.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Which of the Two is Better?? 2 Answers
Faster alternative to GetComponentInParent() method? 1 Answer
How can I adjust a different touch spots on the object 0 Answers