Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by jpierre88 · Apr 08, 2016 at 06:58 PM · colorparticlesystem

ParticleSystem Random Between Two Colors

How can I set "Random Between Two Colors" for startColor in the particle system by script? I couldn't find documentation on it.

alt text

particle.jpg (24.6 kB)
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image martin-3dar · Apr 30, 2016 at 10:09 PM 0
Share

There is an answer to this kind of questions that explains how to do it on the editor.... but who cares about editor? you can edit it by hand.. its the "not needing to build 100 different particle systems" the issue, so not having the second color available sucks. also, i cant figure for the life of me, how the second color would mean a hazard on security that the first color isnt....

avatar image MidasButSilver · Feb 25, 2017 at 10:05 PM 0
Share

Well this question is already a year old, but still only the editor solution is available... since I had the question myself and got here looking for an answer, here is how I figured it out, far more simple and for those who may have the question too:

 //Grab a $$anonymous$$in$$anonymous$$axGradient, or create a new one and assign its values
     ParticleSystem.$$anonymous$$in$$anonymous$$axGradient randomColorGradient = new ParticleSystem.$$anonymous$$in$$anonymous$$axGradient();
     randomColorGradient .mode = ParticleSystemGradient$$anonymous$$ode.TwoColors;
     randomColorGradient .$$anonymous$$Color = Color.blue;
     randomColorGradient .maxColor = Color.red;
     
     //Assign the gradient back to the ParticleSystem
     ParticleSystem.$$anonymous$$ain$$anonymous$$odule main$$anonymous$$odule = particleSystem.main;
     main$$anonymous$$odule .startColor = randomColorGradient ;`
 
avatar image robochase MidasButSilver · Apr 14, 2017 at 06:04 PM 0
Share

thanks for sharing, Loled85. I don't think this solution is possible pre Unity 5.5. just leaving this note here for anybody still using old versions (like me)

3 Replies

· Add your reply
  • Sort: 
avatar image
-1
Best Answer

Answer by TBruce · Apr 08, 2016 at 11:08 PM

@jpierre88 Unfortunately this can not be done as these are private variables. You may be able to do this wile running the game within the editor using SerializedObject and FindProperty. But here is a sample script to show you how it can be done while in the editor (maybe one day Unity will makit so that we can do this at runtime but it is doubtful as it probably makes the game less secure).

 using UnityEngine;
 using System;
 using System.Collections;
 using UnityEditor;
 
 public class TempScript : MonoBehaviour
 {
 
     public ParticleSystem particleSystem;
 
     void Start ()
     {
         SerializedObject so = new SerializedObject(particleSystem);
         if ((so.FindProperty("InitialModule.startColor.minColor") != null) && (so.FindProperty("InitialModule.startColor.maxColor") != null))
         {
             Color colorStart = new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value);
             Color colorEnd = new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value);
 
             so.FindProperty("InitialModule.startColor.minColor").colorValue = Color.Lerp (colorStart, colorEnd, 1);
             
             colorStart = new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value);
             colorEnd = new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value);
 
             so.FindProperty("InitialModule.startColor.maxColor").colorValue = Color.Lerp (colorStart, colorEnd, 1);
             so.ApplyModifiedProperties();
         }
     }
 }
 
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image petersvp · Jun 30, 2018 at 08:57 PM 0
Share

Sorry. You are wrong now.

     var grad = new ParticleSystem.$$anonymous$$in$$anonymous$$axGradient(AshenColorfulGradient);
     grad.mode = ParticleSystemGradient$$anonymous$$ode.RandomColor;
avatar image Bip901 · Apr 10, 2020 at 03:08 PM 0
Share

it is doubtful as it probably makes the game less secure

Security has nothing to do with marking variables private. A private variable is not supposed to be accessed to enforce Object Oriented principles, but you can still access it using reflection. You just shouldn't.

avatar image
2

Answer by petersvp · Jun 30, 2018 at 08:58 PM

The ParticleSystem.MinMaxGradient struct has a mode field. You cas setup the RandomColor value for this field. Like in this code:

     ParticleSystem.MinMaxGradient grad = new ParticleSystem.MinMaxGradient(Color.red, Color.blue);
     grad.mode = ParticleSystemGradientMode.RandomColor;
     // then set this grad variable to some particle module
     mainModule.startColor = grad;

And you have to fill that either into a single gradient or via the two colors constructor.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mattattack2531 · Feb 18, 2019 at 08:43 PM 0
Share

I can't seem to get this to work. Wondered if anything has changed, or if you happen to have found a better method to accomplish the random start color. Thanks.

avatar image zereda-games Mattattack2531 · Feb 18, 2019 at 08:49 PM 0
Share

check out this post from yesterday

https://answers.unity.com/questions/1602916/how-do-i-make-the-scene-background-have-a-default.html?childToView=1602989#answer-1602989

avatar image hussain0305 · Apr 02, 2019 at 10:46 AM 0
Share

Tried this and the particles only come out white, not one of the two colors I'm specifying. Any ideas?

avatar image AnnabelleD hussain0305 · Apr 05, 2019 at 03:08 PM 0
Share

You don't need to set the gradient mode, remove that and it should work

avatar image
0

Answer by Tekksin · Sep 20, 2018 at 06:50 AM

If anybody wants to use Petersvp's method, you can try, but I think its been deprecated. Using his code I got an error, but the error vanished when I did this:

     public Color[] theColor;
     private ParticleSystem ps;
 
 void Start(){
     ps = GetComponent<ParticleSystem> ();
     ParticleSystem.MinMaxGradient grad = new ParticleSystem.MinMaxGradient (theColor [0], theColor [1]);
     grad.mode = ParticleSystemGradientMode.RandomColor;
     var main = ps.main;
     main.startColor = grad;
 }

At the end of the day, I think it's redundant to use startcolor at all, because color overlifetime accounts for the very start and through to the end. If you want just one color, then just set the color over lifetime to the same color twice.

 var col = ps.colorOverLifetime;
             grad.SetKeys (new GradientColorKey[] {
                 new GradientColorKey (color.blue, 0.0f),
                 new GradientColorKey (color.blue, 1.0f)
             }, new GradientAlphaKey[] {
                 new GradientAlphaKey (1.0f, 0.0f),
                 new GradientAlphaKey (1.0f, 1.0f)
             });
             col.color = grad;

hope this helps somebody.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image petersvp · Sep 20, 2018 at 01:20 PM 2
Share

Nothing is deprecated, it's just how your code is structured.

https://docs.unity3d.com/2018.3/Documentation/ScriptReference/ParticleSystem.$$anonymous$$in$$anonymous$$axGradient.html

The debate of StartColor vs ColorOverLifeTime are two separate features. The base color that a particle will be spawned, and then its change over the particle's lifetime. In StartColor you can only feed white with some alpha difference, while the Color Over Lifetime - a gradient that fully disappears, for example. Both StartColor and ColorOverLifetime have their per-particle feature control that is important for some effects.

OP's question was generic, Random between two colors one, and ParticleSystemGradient$$anonymous$$ode.RandomColor is its answer :)

Your answer is, from my point of view, logically same as $$anonymous$$e :) just more full-fledgen example. in my snippet, "main$$anonymous$$odule" is something undefined, and .startColor is just example usage. He can set this $$anonymous$$in$$anonymous$$axGradient on any module, any supported Color-like property :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

52 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to emit particle system attached to object in code? 1 Answer

How can I use 'curves' like ParticleSystem has? 1 Answer

How do you change the colour of individual particles having assigned them a material? 1 Answer

How do I change Particle System color via script in Unity 5.6.1f1? 1 Answer

Change startcolor of particle system through script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges