0

I see how to set a particle's color based on a gradient (e.g. this & this) but I can't find anything on how to get the color.

I have a particle system with the start color set to random with a gradient. I've tried...

Color myColor = myParticleSystem.main.startColor.color

... but it always returns black, regardless of the gradient colors.

I don't see anything in the docs or forums about how to actually get the color that was randomly chosen.

Using Unity 2017.3. Thanks.

Absinthe
  • 3,258
  • 6
  • 31
  • 70
  • I think you **can** get the gradient color with the `ParticleSystem.MinMaxGradient.gradient` property which returns [`Gradient`](https://docs.unity3d.com/ScriptReference/Gradient.html). Have you tried this? – Programmer Feb 16 '18 at 21:37
  • I never found a way to get that (or others) properties from `ParticleSystem.MinMaxGradient` from code, AFAIK the class still hasn't getters available. – Galandil Feb 17 '18 at 12:44

1 Answers1

2

At the moment, we can't read the MinMaxCurve from scripts, as per here: https://blogs.unity3d.com/2016/04/20/particle-system-modules-faq/ (scroll down to the Easing the Pain section).

However, your code returns not a MinMaxCurve, but the Start Color of type Color that you can set via inspector or via script.

For example, if you create a Particle System game object in a scene, and you attach to it this simple script:

using UnityEngine;

public class ParticlesTest : MonoBehaviour {

    ParticleSystem myParticleSystem;
    public Color myColor;

    private void Awake() {
        myParticleSystem = GetComponent<ParticleSystem>();      
    }

    private void Update() {
        myColor = myParticleSystem.main.startColor.color;
    }
}

you can see that myColor changes when you change the Start Color value of the Particle System in Play mode.

Galandil
  • 4,169
  • 1
  • 13
  • 24
  • 2
    "We know that you really ought to be able to read particle system curves from script, regardless of what mode they are in". Doh! – Absinthe Feb 16 '18 at 20:24