C# issue, health, and destroying affect other objects.

I have a script i obtained in a tutorial, it works, but it didnt destroy the enemy after death. I’m trying to destroy a box on my test scene, i used another script to access this one and lower its health just fine. once it becomes 0 it dies and thats what i want. my other box doesnt use its health though, after the first box dissapears, and it was telling me in the error message that im trying to access this script, yet this script was destroyed, i tried other methods and i looked on google and websites for unity and c#, ive been searching for at least 3 days. can somebody help me find out why my script is becoming destroyed on all objects its attached to, and what i need to do to fight and kill each box seperately?

public void Die () {

Destroy(gameObject);

}

is what i believe is the problem, i think i need to find another method of righting this part… if i remove this section, all else works as coded… if i wrote it certain ways, it told me i couldnt change from bolean to int or vice versa?

Any help greatly appreciated
im brand new to this site, been working on this game for months and very slow at learning if im picking things apart on my own, i learn much better with coaching/teaching aid.

using UnityEngine;
using System.Collections;

public class EnemyHealth : MonoBehaviour {    
           
	public int maxHealth = 100;
	public int curHealth = 100;
	

        // Update is called once per frame
	void Update () {
		
		AddjustCurrentHealth(0);
		
		
	}
public void AddjustCurrentHealth(int adj) {
    curHealth += adj;

    if(curHealth <= 0)
       Die();

    if(curHealth > maxHealth)
       curHealth = maxHealth;

}

public void Die () {
    Destroy(gameObject);
}
}

I think maybe the other script you’re using, to turn the health to 0, is still looking for the object once it’s destroyed. Use a check, like:

if(otherScript)
//reduce health

I suspect the other objects are remaining intact, try hitting pause and selecting one to see if it still has the script on it…

I checked, and yes its still showing “EnemyHealth.cs” as being attached to my other box, thats the script above, the script below is “PlayerAttack.cs” which removes the health. but even with both scripts active, EnemyHealth.cs is not destroying the second box.

and thank you for your reply.

using UnityEngine;

using System.Collections;

public class PlayerAttack : MonoBehaviour {
public GameObject target;
public float attackTimer;
public float coolDown;

// Use this for initialization
void Start () {
	attackTimer = 0;
	coolDown = 0.5f;
}

// Update is called once per frame
void Update () {
	if(attackTimer > 0)
		attackTimer -= Time.deltaTime;
	
	if(attackTimer < 0)
		attackTimer = 0;
	
	if(Input.GetKeyUp(KeyCode.J)) {
		if(attackTimer == 0) {
			Attack();
			attackTimer = coolDown;
		}
	}

}

private void Attack() {
	float distance = Vector3.Distance(target.transform.position, transform.position);
	
	Vector3 dir = (target.transform.position - transform.position).normalized;
	
	float direction = Vector3.Dot(dir, transform.forward);
	
	if(distance < 20.5f) {
		if(direction > -0.7) {
			EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
			eh.AddjustCurrentHealth(-10);
		}
	}
}

}

The next question, I guess, is: how are you setting the target? I assume you have the player script attached to the player, with the enemy script attached to each box. There needs to be something to set the variable “target” of the player to the next box. Anyway, you need to choose your target somehow, maybe a raycast since it looks like you want to be facing it to destroy it?

 RaycastHit hit;
    if (Physics.Raycast(transform.position, Vector3.forward, out hit))
{
if(hit.gameObject.tag == "enemy")
target = hit.gameObject;

}

you would need to tag all your boxes as “enemy”. Something like this should set the current target to whichever you’ve most recently faced.