Question by
sagarsamal · Dec 29, 2016 at 02:36 PM ·
enemy health
Enemy clones not destroying after a certain number of clones.
I am making a survival game. Everything is working fine. My enemy prefab is tagged as enemy. Every time enemy health goes to zero they get destroyed and disappear. But after a certain number of clones when their health goes to zero the stop moving but they don't disappear from the scene.
Here is my enemyHealth code :
Animator anim;
AudioSource enemyAudio;
ParticleSystem hitParticles;
CapsuleCollider capsuleCollider;
bool isDead;
bool isSinking;
void Awake ()
{
anim = GetComponent <Animator> ();
enemyAudio = GetComponent <AudioSource> ();
hitParticles = GetComponentInChildren <ParticleSystem> ();
capsuleCollider = GetComponent <CapsuleCollider> ();
currentHealth = startingHealth;
}
void Update ()
{
//if(isSinking)
//{
// transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);
//}
}
public void TakeDamage (int amount, Vector3 hitPoint)
{
if(isDead)
return;
enemyAudio.Play ();
currentHealth -= amount;
hitParticles.transform.position = hitPoint;
hitParticles.Play();
if(currentHealth <= 0)
{
//isDead = true;
//Destroy (GameObject.FindGameObjectWithTag ("Enemy"));
//enemyAudio.clip = deathClip;
//enemyAudio.Play ();
//ScoreManager.score += scoreValue;
Death ();
}
}
void Death ()
{
isDead = true;
//capsuleCollider.isTrigger = true;
//anim.SetTrigger ("Dead");
Destroy (GameObject.FindGameObjectWithTag ("Enemy"));
ScoreManager.score += scoreValue;
enemyAudio.clip = deathClip;
enemyAudio.Play ();
}
//public void StartSinking ()
//{
// GetComponent <NavMeshAgent> ().enabled = false;
//GetComponent <Rigidbody> ().isKinematic = true;
//isSinking = true;
// Destroy (gameObject, 2f);
Comment
Isn't death supposed to destroy yourself? Why are you looking for the first scene object with the tag "Enemy" (I hope there is exactly one in your scene)?
I guess you could simply do
Destroy(gameObject);
ins$$anonymous$$d. Also: The sound will stop when the object is destroyed, so you should probably play it outside of this gameobject (using something like AudioSource.PlayOneShot() or an AudioSource in another gameobject).
Your answer