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

setting particle system startColor's value with gradient or 2 values through C#

Discussion in 'General Graphics' started by marchall_box, Oct 24, 2016.

  1. marchall_box

    marchall_box

    Joined:
    Mar 28, 2013
    Posts:
    139
    Hi,

    I am trying to set startColor of particle system using C# but not a single color.
    Trying to set Gradient or Two Colors.. with script (see the attached image for reference)

    How would I do that?
     

    Attached Files:

    Last edited: Oct 25, 2016
  2. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,224
    Hey,

    In 5.5 this is exposed in the new "main" module. Prior to 5.5, it's unfortunately not possible to set anything other than a solid color to the startColor property. We have a tonne of script examples on the way to demonstrate the new 5.5 functionality, but I just checked and it looks like they haven't been published yet.

    https://docs.unity3d.com/550/Documentation/ScriptReference/ParticleSystem.MainModule.html
    https://docs.unity3d.com/550/Documentation/ScriptReference/ParticleSystem.MainModule-startColor.html

    The old startColor property in the ParticleSystem class has been deprecated, because it does not support Gradients. So, assuming you're on Unity 5.5, you're going to want to do something like:

    Code (CSharp):
    1. ParticleSystem ps = gameObject.GetComponent<ParticleSystem>();
    2. var main = ps.main;
    3. main.startColor = new ParticleSystem.MinMaxGradient(Color.red); // simple color
    4. main.startColor = new ParticleSystem.MinMaxGradient(myGradient); // gradient
    5. main.startColor = new ParticleSystem.MinMaxGradient(Color.red, Color.green); // random between 2 colors
    6. main.startColor = new ParticleSystem.MinMaxGradient(myGradient, myOtherGradient); // random between 2 gradients
    A shorthand version is also available when setting a single color or gradient:

    Code (CSharp):
    1. main.startColor = Color.red;
    2. main.startColor = myGradient;
    Hope it helps!

    PS. Here is the doc page for how to set up a Gradient:
    https://docs.unity3d.com/550/Documentation/ScriptReference/Gradient.html
     
  3. TechDevTom

    TechDevTom

    Joined:
    Oct 21, 2011
    Posts:
    33
    Hey guys, I have just run into this as well, thanks for your help Richard!

    However, at the same time, this new way isn't very self explanatory. Putting down the particle system, then ".startColor" and assigning a colour to it was a great and easy way to go about changing the colour of your particle system. I understand that 5.5 has brought a lot of new features in and the error messages provided by unity are a good guide to how to solve the colour issue, but could you explain why we now have to assign a temporary variable in order to change things like the startColour?

    Cheers, I'd really appreciate any info you could provide because it will help me understand things better and become a better Unity programmer :)
     
  4. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,224
    The approach avoids allocating any garbage. we're working on removing the need for it, in a future version on Unity - we agree that it's not ideal.
     
    karl_jones likes this.
  5. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,665