- Home /
Question by
Dracolyte · Jul 27, 2020 at 08:38 PM ·
2dgameobjectparticlesdestroyparticlesystem
How to destroy particle gameobject after its finished?
I cant get it to work, that the remaining particle gameobject destroys after its finished. I made a particle effect using Unitys particle system. It instantiates, when the enemy dies. My problem is, that i cant get rid of the remaining particle gameobject after the particle effect is done.
My code:
void Update()
{
Vector3 direction = player.position - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg -90f;
transform.rotation = Quaternion.Euler(0, 0, angle);
direction.Normalize();
movement = direction;
moveEnemy(direction);
}
void moveEnemy(Vector2 direction)
{
rb.MovePosition((Vector2)transform.position + (direction * speed));
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Bullet")
{
Destroy(collision.gameObject);
if (HP >= 0)
{
StartCoroutine("ChangeColorOnHit");
}
HP--;
if(HP <= 0)
{
Destroy(gameObject);
ParticleSystem particleclone = Instantiate(deathparticle, transform.position, Quaternion.identity);
//Destroy particle if its finished
}
}
}
IEnumerator ChangeColorOnHit()
{
mat.color = colorOnHit;
yield return new WaitForSeconds(0.1f);
mat.color = defaultcolor;
}
Comment
Answer by bboydaisuke · Jul 28, 2020 at 02:34 AM
You don't need code.
Find "Stop Action" from Particle System Main Module.
2020-07-28-11h31-03.png
(28.4 kB)
Answer by deniskotpletnev · Jul 27, 2020 at 09:18 PM
Create a new script and assign it to the Particle System
public class ParticleSystemAutoDestroy : MonoBehaviour {
private ParticleSystem _ps;
public void Start()
{
_ps = GetComponent<ParticleSystem>();
}
public void FixedUpdate()
{
if (_ps && !_ps.IsAlive())
{
Destroy(gameObject);
}
}
}