Search code examples
unity-game-engineparticle-systemparticles

How to read and save start color of a Particle System in Unity


Lets say that I have a Particle system with start color as random between two colors say black and white. Now I know that the colors can be changed using MinMaxGradient. But how do I save the original start colors so I can use them later.


Solution

  • Use ParticleSystem.MainModule.startColor.

    This will return a MinMaxGradient which has a colorMin property. This is what you should save in a class field for future reuse. For example, if you initialize your ParticleSystem in the Start method:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class MyScript : MonoBehaviour
    {
        //declare class fields for your particle system and start color
        private ParticleSystem myParticleSystem;
        private Color particleStartColor;
    
        void Start()
        {
            //...
            myParticleSystem = GetComponent<ParticleSystem>();
            ParticleSystem.MinMaxGradient myMinMaxGradient = myParticleSystem.main.startColor;
            particleStartColor = myMinMaxGradient.colorMin;
            //...  
        }
    }