- Home /
Help! This script is causing my Enemy to die on Spawn??
Hello,
I have been having issues with a simple script, it seems to be causing my enemies to die on spawn please help, am new to coding. using UnityEngine; using System.Collections;
public class DamageHandlerEnemy : MonoBehaviour {
public int maxHealth = 100;
[System.Serializable]
public class EnemyStats {
public int maxHealth;
private int _curHealth;
public int curHealth
{
get { return _curHealth; }
set { _curHealth = Mathf.Clamp (value, 0, maxHealth); }
}
public void Init()
{
curHealth = maxHealth;
}
}
public float invulnPeriod = 0;
float invulnTimer = 0;
int correctLayer;
SpriteRenderer spriteRend;
public EnemyStats stats = new EnemyStats ();
[Header ("Optional: ")]
[SerializeField]
private StatusIndicator statusIndicator;
void Start() {
correctLayer = gameObject.layer;
// NOTE! This only get the renderer on the parent object.
// In other words, it doesn't work for children. I.E. "enemy01"
spriteRend = GetComponent<SpriteRenderer>();
if(spriteRend == null) {
spriteRend = transform.GetComponentInChildren<SpriteRenderer>();
if(spriteRend==null) {
Debug.LogError("Object '"+gameObject.name+"' has no sprite renderer.");
stats.Init();
if (statusIndicator != null)
{
statusIndicator.SetHealth (stats.curHealth, stats.maxHealth);
}
}
}
}
void OnTriggerEnter2D() {
EnemyStats stats = new EnemyStats ();
stats.curHealth--;
if(invulnPeriod > 0) {
invulnTimer = invulnPeriod;
gameObject.layer = 10;
}
}
void Update() {
if(invulnTimer > 0) {
invulnTimer -= Time.deltaTime;
if(invulnTimer <= 0) {
gameObject.layer = correctLayer;
if(spriteRend != null) {
spriteRend.enabled = true;
}
}
else {
if(spriteRend != null) {
spriteRend.enabled = !spriteRend.enabled;
}
}
}
EnemyStats stats = new EnemyStats ();
if(stats.curHealth <= 0) {
Die();
if (statusIndicator != null)
{
statusIndicator.SetHealth (stats.curHealth, stats.maxHealth);
}
}
}
void Die() {
Destroy(gameObject);
}
}
Answer by ScaniX · Jul 25, 2016 at 08:37 AM
I think the line 85 is there by accident:
85: EnemyStats stats = new EnemyStats ();
You already have an instance in your member variable that got initialized in your Start()
method. I cannot see why you would want to create a local variable with a new instance here. :)
The same goes for the same code line in your OnTriggerEnter2D()
method.
Also: You are never setting your maxHealth
member in the EnemyStats
class. You have a member in the outer class that is set to 100, but the member of the stats class stays 0.
You should probably just create ONE constant in the outer class like this:
public static int maxHealth = 100; // or const if it is never changed from somewhere
And then use it everywhere with
DamageHandlerEnemy.maxHealth
Also that OnTriggerEnter2D has no filter, so anything entering the trigger area is going to reduce the enemies health. ick.
Answer by tanoshimi · Jul 25, 2016 at 06:09 AM
Float values are initialised to zero by default. Therefore your problem is here:
EnemyStats stats = new EnemyStats ();
if(stats.curHealth <= 0) {
Die();
Unless your constructor states otherwise, the curHealth value of your newly-created stats class is 0. So you're destroying the gameobject immediately after.
Yes. And even after changing the value in the constructor you should not call 'EnemyStats stats = new EnemyStats ();' right before checking the curHealth in the Update()-function ... as the value will always be the one of the constructor in the next line (87) and 'stats.curHealth
Your answer
Follow this Question
Related Questions
Counting prefabs in screen?,Problem with counting prefabs in screen 1 Answer
how to allow the key to only open 1 door rather than all of them? 0 Answers
(PLEASE HELP) hi guys! i have a problem with my Score text 1 Answer
First Person Controller - Problems with footstep audio being played too fast. 0 Answers