Break while WaitForSeconds C#

using UnityEngine; using System.Collections;

public class ParticleScript : MonoBehaviour { public static Color [] holder ;

public static bool PlayerMoved = false
;

void Update()
{
    if (Mathf.Abs(PlayerMoveScript.MoveForward) > 0)
    {
        PlayerMoved = true;
    }
    else PlayerMoved = false;
}

public static IEnumerator FadeParticles()
{
    Debug.Log("Done Waiting");

        if (PlayerBias.theRedParticles != null) 
        {
            holder = new Color[5]; // we have five color groups to alter

            // Reset our Emitter coordinate and assign it to follow the player
            theRedParticles.GetComponent<ParticleEmitter>( ).transform.position = new Vector3(0, 0, 0);
            theRedParticles.GetComponent<ParticleEmitter>( ).transform.position = PlayerMoveScript.myPosition;

            holder = theRedParticles.GetComponent<ParticleAnimator>( ).colorAnimation;

            if (Mathf.Abs(PlayerMoveScript.MoveForward) > 0)
            {                   
                for (int x=0; x<5; x++) 
                holder[x].a -= 0.02f;
            }

            PlayerMoveScript.lastPosition = PlayerMoveScript.myPosition; // Update our Position 
            theRedParticles.GetComponent<ParticleAnimator>( ).colorAnimation = holder;

            if (holder[0].a < 0)
            {   
                Debug.Log("Clearing Particles..");
            theRedParticles.GetComponent<ParticleEmitter>( ).ClearParticles();
            }
        }
        if (theRedParticles != null) // Meaning that we instantiated it with PlayerBias
        {
            holder = new Color[5]; // we have five color groups to alter

            // Reset our Emitter coordinate and assign it to follow the player
            theRedParticles.GetComponent<ParticleEmitter>( ).transform.position = new Vector3(0, 0, 0);
            theRedParticles.GetComponent<ParticleEmitter>( ).transform.position = PlayerMoveScript.myPosition;

            if (Mathf.Abs(PlayerMoveScript.MoveForward) > 0)
            {                   
                for (int x=0; x<5; x++) 
                holder[x].a -= 0.02f;
            }

            PlayerMoveScript.lastPosition = PlayerMoveScript.myPosition; // Update our Position 

        }
            yield return new WaitForSeconds(3.5f);
    }
}

Did you over-simplify your example? Is there other code you're calling?

Update Totally changed the code sample :)

bool startFade = false; // new variable.
bool stopFade = false; // another new variable

void OnTriggerStay(Collider other){
    ParticleInstantiation(); // spelling changed :)
    FadeParticles();
}
void OnTriggerExit(Collider other){
    theRedParticles = null; // will this work instead?
}
void FadeParticles(){
    //... Fadeparticle is unchanged, except for these two lines below:
    // theRedParticles.particleEmitter.emit = false;
    startFade = true; // replaces line above.
    //... 
}
private IEnumerator Wait() {
    const int emitOnTime = 22;
    const int emitOffTime = 20;
    const float ticks = 0.1f; // tenth of a second.

    while (theRedParticles != null) {
        theRedParticles.particleEmitter.emit = true;
        for (int x = 0; x < emitOnTime; x++) {
            if (startFade) {
                startFade = false;
                break; // breaks out of the for loop, goes to "emit = false;".
            }
            yield return new WaitForSeconds(ticks);
        }
        theRedParticles.particleEmitter.emit = false;
        for (int x = 0; x < emitOffTime; x++) {
            if (stopFade) {
                stopFade = false;
                break; // breaks out of no-emit loop.
            }
            yield return new WaitForSeconds(ticks);
        }
    }
    yield break;
}

Basically, I re-wrote the Wait() co-routine to: not use recursion :) - instead, it's in a while(tRP exists) loop. It uses two for loops, the first emits for 2.2 seconds, the second no-emits for 2 seconds. It will keep doing this as long as tRP exists. There is a new variable, startFade, which is set by FadeParticles - when set, it stops the emit phase of Wait(), and begins the no-emits phase (when I presume particles decay?).

In order to make the loop interruptible, I set it to WaitForSeconds() in tenths of a second. So if startFade() tells it to, it will begin fading within a tenth of a second. It will then continue fading for 2 seconds, and then begin the cycle of emitting/not emitting, until the next interruption.

Again, I'm not entirely sure this is the logic you want, it still isn't entirely clear to me. :) But it might work. Let me know if you have any questions, or if it's not right.

Also, in the OnTriggerExit, what would happen if you just set tRP to null immediately? I'm not entirely sure about Unity's garbage collection, but I think it would stop the particle-emitting at the end of the frame.

Oh, and another change to the code is doing emit = true; instead of re-instantiating the Particle-emitter just to turn it on. :)