Skip to content

Instantly share code, notes, and snippets.

@wakeup5
Created January 10, 2020 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wakeup5/db3b3b84c258514410474c8dc017d9a4 to your computer and use it in GitHub Desktop.
Save wakeup5/db3b3b84c258514410474c8dc017d9a4 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ParticleSystemLifetime : MonoBehaviour
{
public enum Action
{
Disable,
Destroy
}
[SerializeField]
public Action action;
private ParticleSystem ps;
private void Awake()
{
ps = GetComponent<ParticleSystem>();
}
private void OnEnable()
{
ps.Play();
StartCoroutine(nameof(DoActionAfterLifetime));
}
private void OnDisable()
{
StopCoroutine(nameof(DoActionAfterLifetime));
}
private IEnumerator DoActionAfterLifetime()
{
yield return new WaitForSeconds(ps.main.duration);
switch (action)
{
case Action.Disable:
gameObject.SetActive(false);
break;
case Action.Destroy:
Object.Destroy(gameObject);
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment