How to store array value with GetComponent<ParticleSystem> ().emission.rateOverTime?

Hi, I know how to set up the rateOverTime of ParticleSystem and its other properties, but I don’t know how to store the initial value of rateOverTime to other variables before I turn them to zero, so I can toggle them.

Set ParticleSystem > emission > rateOverTime to zero:

/* ProjectileController.cs */
void OnCollisionEnter2D (Collision2D other) //void OnCollisionStay2D (Collision2D other)
{
    if (!enable) return;
    
    if (other.gameObject.CompareTag ("Enemy"))
    {
        foreach (ParticleSystem thisBulletTail in bulletTail)
        {
            var em = thisBulletTail.GetComponent<ParticleSystem> ().emission;
            em.rateOverTime = 0f;
        }
  }
}

But first, I want their initial value to be stored in array (with float?) in Start ()…

How can I do that if their type is like this:

var PS_1 = bulletTail[0].GetComponent<ParticleSystem> ();
Debug.Log ("ParticleSystem: " + PS_1.GetType());

var PS_2 = bulletTail[0].GetComponent<ParticleSystem> ().emission;
Debug.Log ("PS.emission: " + PS_2.GetType());

var PS_3 = bulletTail[0].GetComponent<ParticleSystem> ().emission.rateOverTime;
Debug.Log ("PS.emission.rateOverTime: " + PS_3.GetType());

var PS_4 = bulletTail[0].GetComponent<ParticleSystem> ().emission.rateOverDistance;
Debug.Log ("PS.emission.rateOverDistance: " + PS_4.GetType());

So this won’t do:

float[] rateOverDistance;

And there’s no:

var[] rateOverDistance;

Is there easy way to do this? I can’t just deactivate the whole object, because the particle effects will also be hidden. I just plan to deactivate some components: SpriteRenderer, ProjectileController, and ParticleSystem’s emission’s rateOverDistance, and toggle them as I pooling them to recycle. Not destroying or deactivate the gameObject.

Thank you! :wink:



3331544--259703--TestType_ParticleSystem.png

For any of those you can “save” them by simply assigning them to a local instance of that same structure, a MinMaxCurve in the cases you cited above.

ParticleSystem.MinMaxCurve savedCurve = em.rateOverTime;

And obviously, just assign it back when you want to restore it.

1 Like

Thank you, Kurt-Dekker…

I see, it does not give me an error:

/* ProjectileController.cs */

public ParticleSystem[] bulletTail;
private ParticleSystem.MinMaxCurve[] em;

public ParticleSystem.MinMaxCurve GetInitialEmissionROT (int index)
{
    if (index >= 0 && index < em.Length)
        return em[index];
    else
        return em[0];
}

void Start ()
{
    em = new ParticleSystem.MinMaxCurve[bulletTail.Length];
    for (int i = 0; i < bulletTail.Length; i++)
    {
        em[i] = bulletTail[i].GetComponent<ParticleSystem> ().emission.rateOverTime;
    }
}

//...

void OnCollisionEnter2D (Collision2D other) {
    //...
 
    //Destroy (gameObject);
    GetComponent<ProjectileController> ().enabled = false;
    GetComponent<SpriteRenderer> ().enabled = false;

    //rateOverTime to zero
    foreach (ParticleSystem thisBulletTail in bulletTail)
    {
        var emToZero = thisBulletTail.GetComponent<ParticleSystem> ().emission;
        emToZero.rateOverTime = 0f;
    }
 
    //...
}

/* PlayerAbilityController.cs / AIFireController.cs */

    //Instantiate (gameObject);
    projectile.Controller.GetComponent<ProjectileController> ().enabled = true;
    projectile.GetComponent<SpriteRenderer> ().enabled = true;

    //rateOverTime restore
    for (int i = 0; i < projectile.bulletTail.Length; i++)
    {
        var emRestore = projectile.bulletTail[i].GetComponent<ParticleSystem> ().emission;
        emRestore.rateOverTime = projectile.GetInitialEmissionROT (i);
    }

Though, there still many things I need to do, to say this really gonna work; I need to re-code it from Instantiate-Destroy to Object-Pooling per unique projectiles.

1 Like