- Home /
My "deathEffect" doesn't disappears after use, can you help me?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Enemy : MonoBehaviour { public int health = 100;
public GameObject deathEffect;
public void TakeDamage(int damage) { health -= damage;
if (health <= 0)
{
Die();
}
}
void Die ()
{
Instantiate(deathEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
Answer by unity_HTr7mOeTIRzvVA · Dec 22, 2020 at 02:53 PM
Store the instatiated death effect object to a variable so you will be able to call to it again to destroy it.
GameObject newDeathEffect = Instantiate(deathEffect, transform.position, Quaternion.identity);
this.gameObject.SetActive(false);
Destroy(newDeathEffect, 5f);
Destroy(this.gameObject, 5f);
Or Create a script and put it on your death effect, set a timer and destroy it. @nasosk09
Did it but just says: All compiler errors have to be fixed before you can enter playmode! UnityEditor.SceneView:ShowCompileErrorNotification ()
Answer by nasosk09 · Dec 22, 2020 at 07:56 PM
Because I am new in Unity can you tell me where do I precisely put it?
@nasosk09 Put it inside your Die() method
using UnityEngine;
public class Enemy : $$anonymous$$onoBehaviour {
public int health = 100;
public GameObject deathEffect;
public void TakeDamage(int damage){
health -= damage;
if(health <= 0){
Die();
}
}
void Die(){
GameObject newDeathEffect = Instantiate(deathEffect, transform.position, Quaternion.identity); // store it in a variable
this.gameObject.SetActive(false); // disable your object because it is dead
Destroy(newDeathEffect, 5f); // destroy the stored new death effect, but after 5 seconds (change the number to make the duration longer or shorter)
Destroy(this.gameObject, 5f); // destroy to disappear from hierarchy, but after 5 seconds (change the number to make the duration longer or shorter)
}
}
This: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Enemy : $$anonymous$$onoBehaviour { public int health = 100;
public GameObject deathEffect;
public void TakeDamage(int damage) { health -= damage;
if (health <= 0)
{
Die();
}
}
void Die ()
{
GameObject newDeathEffect = Instantiate(deathEffect, transform.position, Quaternion.identity);
this.gameObject.SetActive(false);
Destroy(newDeathEffect, 5f);
Destroy(this.gameObject, 5f);
}
}
doesn't work for me
Your answer