Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by Ultimate360, Dec 22, 2017.

  1. Ultimate360

    Ultimate360

    Joined:
    Oct 28, 2016
    Posts:
    27
    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:
    Code (CSharp):
    1. /* ProjectileController.cs */
    2. void OnCollisionEnter2D (Collision2D other) //void OnCollisionStay2D (Collision2D other)
    3. {
    4.     if (!enable) return;
    5.    
    6.     if (other.gameObject.CompareTag ("Enemy"))
    7.     {
    8.         foreach (ParticleSystem thisBulletTail in bulletTail)
    9.         {
    10.             var em = thisBulletTail.GetComponent<ParticleSystem> ().emission;
    11.             em.rateOverTime = 0f;
    12.         }
    13.   }
    14. }
    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:



    Code (CSharp):
    1. var PS_1 = bulletTail[0].GetComponent<ParticleSystem> ();
    2. Debug.Log ("ParticleSystem: " + PS_1.GetType());
    3.  
    4. var PS_2 = bulletTail[0].GetComponent<ParticleSystem> ().emission;
    5. Debug.Log ("PS.emission: " + PS_2.GetType());
    6.  
    7. var PS_3 = bulletTail[0].GetComponent<ParticleSystem> ().emission.rateOverTime;
    8. Debug.Log ("PS.emission.rateOverTime: " + PS_3.GetType());
    9.  
    10. var PS_4 = bulletTail[0].GetComponent<ParticleSystem> ().emission.rateOverDistance;
    11. Debug.Log ("PS.emission.rateOverDistance: " + PS_4.GetType());
    So this won't do:
    Code (CSharp):
    1. float[] rateOverDistance;
    And there's no:
    Code (csharp):
    1. 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! ;)
     

    Attached Files:

    Last edited: Dec 22, 2017
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    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.

    Code (csharp):
    1. ParticleSystem.MinMaxCurve savedCurve = em.rateOverTime;
    And obviously, just assign it back when you want to restore it.
     
    Ultimate360 likes this.
  3. Ultimate360

    Ultimate360

    Joined:
    Oct 28, 2016
    Posts:
    27
    Thank you, Kurt-Dekker...

    I see, it does not give me an error:

    /* ProjectileController.cs */
    Code (CSharp):
    1. public ParticleSystem[] bulletTail;
    2. private ParticleSystem.MinMaxCurve[] em;
    3.  
    4. public ParticleSystem.MinMaxCurve GetInitialEmissionROT (int index)
    5. {
    6.     if (index >= 0 && index < em.Length)
    7.         return em[index];
    8.     else
    9.         return em[0];
    10. }
    11.  
    12. void Start ()
    13. {
    14.     em = new ParticleSystem.MinMaxCurve[bulletTail.Length];
    15.     for (int i = 0; i < bulletTail.Length; i++)
    16.     {
    17.         em[i] = bulletTail[i].GetComponent<ParticleSystem> ().emission.rateOverTime;
    18.     }
    19. }
    20.  
    21. //...
    22.  
    23. void OnCollisionEnter2D (Collision2D other) {
    24.     //...
    25.  
    26.     //Destroy (gameObject);
    27.     GetComponent<ProjectileController> ().enabled = false;
    28.     GetComponent<SpriteRenderer> ().enabled = false;
    29.  
    30.     //rateOverTime to zero
    31.     foreach (ParticleSystem thisBulletTail in bulletTail)
    32.     {
    33.         var emToZero = thisBulletTail.GetComponent<ParticleSystem> ().emission;
    34.         emToZero.rateOverTime = 0f;
    35.     }
    36.  
    37.     //...
    38. }
    /* PlayerAbilityController.cs / AIFireController.cs */
    Code (CSharp):
    1.     //Instantiate (gameObject);
    2.     projectile.Controller.GetComponent<ProjectileController> ().enabled = true;
    3.     projectile.GetComponent<SpriteRenderer> ().enabled = true;
    4.  
    5.     //rateOverTime restore
    6.     for (int i = 0; i < projectile.bulletTail.Length; i++)
    7.     {
    8.         var emRestore = projectile.bulletTail[i].GetComponent<ParticleSystem> ().emission;
    9.         emRestore.rateOverTime = projectile.GetInitialEmissionROT (i);
    10.     }
    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.
     
    Kurt-Dekker likes this.