Question by
kymbers · Jan 11, 2021 at 02:14 PM ·
enemyenemy aienemiesenemy healthenemy damage
How To Destroy/Deactivate one enemy without affecting other enemies of the same type?
Hi, I have a problem, I have a universal enemy script, which I use on all enemies, but as in the script when an enemy dies it is set to deactivate the current script and because of that the first enemy goes perfectly to kill him, but when I arrive at the second I can't attack him because he says the script is disabled and he can't access it, how can I do when I kill an enemy not to affect the main script of all enemies?
public void TakeDamage(int damage)
{
currentHealth -= damage;
anim.SetTrigger("TakeHit");
if (currentHealth == 80)
{
heartRightOne.SetActive(false);
}
else if (currentHealth == 60)
{
heartRightOne.SetActive(false);
heartRightTwo.SetActive(false);
}
else if (currentHealth == 40)
{
heartRightOne.SetActive(false);
heartRightTwo.SetActive(false);
heartMiddle.SetActive(false);
}
else if (currentHealth == 20)
{
heartRightOne.SetActive(false);
heartRightTwo.SetActive(false);
heartMiddle.SetActive(false);
heartLeftTwo.SetActive(false);
}
else if (currentHealth <= 0)
{
Die();
heartRightOne.SetActive(false);
heartRightTwo.SetActive(false);
heartMiddle.SetActive(false);
heartLeftTwo.SetActive(false);
heartLeftOne.SetActive(false);
}
//Check if the enemy is facingRight or not to display the damage text popup;
if (facingRight == true)
{
GameObject go = Instantiate(damageText, transform.position, Quaternion.identity);
go.GetComponent<TextMesh>().text = damage.ToString();
go.transform.SetParent(gameObject.transform);
go.transform.localPosition += new Vector3(Random.Range(-textPositionX, textPositionX), Random.Range(+textPositionY, 2), Random.Range(0, 0));
}
else if (!facingRight)
{
GameObject go = Instantiate(damageText, transform.position, Quaternion.identity);
go.GetComponent<TextMesh>().text = damage.ToString();
go.transform.SetParent(gameObject.transform);
go.transform.localPosition += new Vector3(Random.Range(-textPositionX, textPositionX), Random.Range(+textPositionY, 2), Random.Range(0, 0));
}
}
public void Die()
{
//Set The Animation For Dead and Stop all other functions / animations;
anim.SetBool("Dead", true);
//Instantiate a Gold Prefab;
GameObject gold = Instantiate(goldPrefab, transform.position, Quaternion.identity);
gold.transform.SetParent(gameObject.transform);
gold.transform.localPosition = new Vector2(0, goldPositionY);
gold.transform.localScale = new Vector2(localScaleX, localScaleY);
//Set Player +25 Experience;
experience.IncraeseExp(25);
//Deactivate The Enemy Collider;
GetComponent<Collider2D>().enabled = false;
//Destroy The Enemy AI;
Destroy(hotZone);
Destroy(triggerArea);
Destroy(hitBox);
Destroy(enemyHearts);
//Deactivate The Current Enemy Script;
this.enabled = false;
}
Because the script is done this way, when I go to attack the second enemy, I can't kill him anymore If help I can make a video to show the entire script bcs its a big one and to show exactly the problem
Comment