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. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Particle System Random Color in C#.

Discussion in 'General Graphics' started by username132323232, Nov 26, 2018.

  1. username132323232

    username132323232

    Joined:
    Dec 9, 2014
    Posts:
    472
    In Particle System main module, there is an option "Random Color" for Start Color. Is it possible to set it in C#? Thanks.
     
  2. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,166
  3. username132323232

    username132323232

    Joined:
    Dec 9, 2014
    Posts:
    472
    Thank you for the reply. I must be doing something wrong, but what I get doesn't look like random color. Each particle gets its color from left to right. So, if I have a 10-seconds particle system, I get green circles for the first 5 seconds and red circles for the next 5 seconds. This seems to be regular gradient not random color behavior. Here is my code:

    Code (CSharp):
    1.        ParticleSystem currentParticleSystem = instance.GetComponent<ParticleSystem>();
    2.         ParticleSystem.MainModule psMain = currentParticleSystem.main;
    3.  
    4.         psMain.startColor = new ParticleSystem.MinMaxGradient(particleSystemGradient);
    5.         var mode = psMain.startColor.mode;
    6.         mode = ParticleSystemGradientMode.RandomColor;
    7.  
     
  4. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,166
    You are only changing your local variable "mode" by doing it that way. You need to assign it back to the MinMaxGradient. But that type is a struct so the obvious code won't work:
    Code (CSharp):
    1. psMain.startColor.mode = ParticleSystemGradientMode.RandomColor;
    Instead, rewrite the whole thing like this:
    Code (CSharp):
    1. var randomColors = new ParticleSystem.MinMaxGradient(particleSystemGradient);
    2. randomColors.mode = ParticleSystemGradientMode.RandomColor;
    3. psMain.startColor = randomColors;
     
  5. username132323232

    username132323232

    Joined:
    Dec 9, 2014
    Posts:
    472
    That's beautiful! Thank you Richard!
     
    richardkettlewell likes this.
  6. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,732
    Hi I'm stuck with a similar problem, just trying to set two random start colours for a particle system.
    Code (CSharp):
    1.             var main = myParticleSystem.main.startColor;
    2.             main.mode = ParticleSystemGradientMode.TwoColors;
    3.             main.colorMax = Color.red;
    4.             main.colorMax = Color.blue;
    Should something like that work?
    Thanks,
    Pete
     
    Last edited: Mar 29, 2019
  7. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,166
    You used colorMax twice and you didn't reassign the gradient back to the startColor property of the particle system. You could fix it up, but a simpler way to write this is to use the ParticleSystemMinMaxGradient constructor that takes 2 colors and sets the mode/colors up for you:
    Code (CSharp):
    1. var main = myParticleSystem.main;
    2. main.startColor = new ParticleSystem.MinMaxGradient(Color.red, Color.blue);
     
  8. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,732
    richardkettlewell likes this.
  9. Livealot

    Livealot

    Joined:
    Sep 2, 2013
    Posts:
    228
    This gem should get into the scripting docs
     
    HardK and TorchFireGames like this.
  10. Kaldrin

    Kaldrin

    Joined:
    Jul 10, 2018
    Posts:
    41
    Thank you !
    I was wondering why I couldn't reassign the particleSystem.MainModule as it was only read-only, but it's a reference variable so it works fine actually!
     
    richardkettlewell likes this.
  11. unity_PwXfMGmzHmDcsw

    unity_PwXfMGmzHmDcsw

    Joined:
    Jul 7, 2019
    Posts:
    6
    Hi Richard, when I do this is says "particleSystemGradient" doesn't exist in this context. I have googled trying to find a reference to this "particleSystemGradient" but can't, or rather is just points me back to the unhelpful documentation.
     
  12. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,540
    The
    particleSystemGradient
    was a variable which @username132323232 had in their own code, not something Unity provided, and then @richardkettlewell continued to use that name because it was working for them.

    Declare a variable in your class of this type, and use it. Change the colors as desired in the Inspector.

    public Gradient particleSystemGradient;


    https://docs.unity3d.com/ScriptReference/Gradient.html