Hey there,
just wanted to share a bit of code.
In this lecture we had many
if (!particleSuccess.isPlaying) particleSuccess.Play();
calls, and the same for all the stop calls… This didn’t look too nice in the code, so I made an extension class for the ParticleSystem, which solves this:
using UnityEngine;
namespace ProjectBoost
{
public static class ParticleExtensions
{
public static void PlaySafe(this ParticleSystem sys) {
if (!sys.isPlaying) sys.Play();
}
public static void StopSafe(this ParticleSystem sys) {
if (sys.isPlaying) sys.Stop();
}
}
}
Just create a “ParticleExtensions.cs” file anywhere in your scripts folder and copy the code to it.
You then can safely start and stop particle systems without having to care whether they are currently playing or not, by using the PlaySafe()
method:
// instead of
if (thrustMainParticles.isPlaying) thrustMainParticles.Stop();
// you can simply write
thrustMainParticles.StopSafe();
the extension does the “if…” for you
cheers
Mike
PS: same can be done with AudioSources