3

I am trying to change the "startColor" field of the ParticleSystem in Unity 2017.

I try to write the code in 2 different ways in C#.

The first way is:

ParticleSystem.MainModule settings =
GetComponent<ParticleSystem>().main;

settings.startColor = new Color(9, 251, 122, 128);

and the second way is:

GetComponent<ParticleSystem>().startColor  =  new Color(9, 251, 122, 128);

But, in both cases, when I run the code, the startColor is automatically set to WHITE, which is (255, 255, 255, 128).

It seems that the code above used to work in older versions of Unity. But, in Unity 2017, it fails to change "startColor" properly.

Please let me know how to fix it. Thanks.

PS:

Here is the FULL original question and answer (with correct C# syntax) inside Unity Forum: https://answers.unity.com/questions/604246/how-to-change-the-color-of-particle-system.html

Please note that it seems that solution may work well for older versions of Unity and does not work with Unity 2017 (unless I am mistaken).

Job_September_2020
  • 858
  • 2
  • 10
  • 25
  • 1
    Double check it's not set anywhere in the inspector because it will override your script! – Daxtron2 May 31 '18 at 12:23
  • Where do you execute this code? In start? awake? some function? – MX D May 31 '18 at 12:31
  • I put this code inside the Update() method. I can see that the Update method is called frequently as I put Debug.Log() inside that method. So, I am sure that the code is called as frequently as the Update() method. (The reason, I put the code inside Update() is that later on, I want to change the color every 2 or 3 seconds.) – Job_September_2020 May 31 '18 at 12:33
  • @TJ Wolschon: Inside the inspector for the Particle System, initially I set the startColor to GREEN. Now, if I don't call my script, then the startColor stays GREEN, which is correct. But, as soon as, I call my script, the startColor changes to WHITE, which wrong because I do not set the startColor to WHITE in my script shown above. – Job_September_2020 May 31 '18 at 12:37
  • *"How do you type "ParticleSystem" inside "<>" in StackOverFlow ? "* Put the code between two **`** or the curly braces button on the screen to insert it yourself. See [this](https://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks) post – Programmer May 31 '18 at 13:01
  • Thanks for the tips. That is helpful :-) – Job_September_2020 May 31 '18 at 13:11

2 Answers2

4

This fairly normal, considering Color uses floating point colors, taking values ranging from 0 to 1.

As your values are rounded, they all end up at 1, which in your case leads to pure white.

If you wish to use 0-255 ranged colors, you should use Color32 instead.

MX D
  • 2,453
  • 4
  • 35
  • 47
3

It seems that the code above used to work in older versions of Unity. But, in Unity 2017, it fails to change "startColor" properly.

I am even surprised that it worked at-all in the previous version. Note that what you have is an undefined behavior.

Color takes 0 to 1 value and Color32 takes 0 to 255 value range.

You can still use 0 to 255 range with Color but divide it by 255

settings.startColor = new Color(9 / 255f, 251 / 255f, 122 / 255f, 128 / 255f);

Or use create Color from Color32

Color color = new Color32(9, 251, 122, 128);
settings.startColor = color;
Programmer
  • 121,791
  • 22
  • 236
  • 328