Search code examples
unity-game-engineparticle-systemparticles

How to stop particle system with a timer?


so I have a simple code that Spawn particle when player stay at a trigger. But the particles keeping turn on and i want to turn off after a few seconds. What i have to do?

 if (other.gameObject.tag == "Player" && Input.GetKeyDown(KeyCode.E))
        {
            PlayerManager.health += 1;
            MyParticleEffect.SetActive(true);

            Debug.Log("e key was pressed");
        }

Solution

  • If you dont want to change this value you can turn off the loop property and play with the duration.

    If you want to change this dynamically, its best to use StartCoroutine: https://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html

    This is used like a timer, and you can turn the particle system off when the timer elapses.