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);
	}

}

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. :slight_smile:

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

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.