Changing color and size over lifetime helps make effects look alive and natural. It adds smooth changes as things grow or fade.
Color and size over lifetime in Unity
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Unity
ParticleSystem.MainModule main = particleSystem.main; main.startColor = new ParticleSystem.MinMaxGradient(Color.white, Color.red); main.startSize = new ParticleSystem.MinMaxCurve(1f, 3f); ParticleSystem.ColorOverLifetimeModule colorOverLifetime = particleSystem.colorOverLifetime; colorOverLifetime.enabled = true; colorOverLifetime.color = new ParticleSystem.MinMaxGradient( new Gradient() { colorKeys = new GradientColorKey[] { new GradientColorKey(Color.white, 0f), new GradientColorKey(Color.red, 1f) }, alphaKeys = new GradientAlphaKey[] { new GradientAlphaKey(1f, 0f), new GradientAlphaKey(0f, 1f) } } ); ParticleSystem.SizeOverLifetimeModule sizeOverLifetime = particleSystem.sizeOverLifetime; sizeOverLifetime.enabled = true; sizeOverLifetime.size = new ParticleSystem.MinMaxCurve(1f, AnimationCurve.Linear(0f, 1f, 1f, 0f));
Use colorOverLifetime.enabled = true to activate color changes over time.
Gradients define how color and transparency change from start (0) to end (1) of particle life.
Examples
Unity
var colorOverLifetime = particleSystem.colorOverLifetime; colorOverLifetime.enabled = true; colorOverLifetime.color = Color.red;
Unity
var sizeOverLifetime = particleSystem.sizeOverLifetime; sizeOverLifetime.enabled = true; sizeOverLifetime.size = AnimationCurve.Linear(0f, 0.5f, 1f, 2f);
Unity
Gradient gradient = new Gradient();
gradient.colorKeys = new GradientColorKey[] {
new GradientColorKey(Color.yellow, 0f),
new GradientColorKey(Color.black, 1f)
};
gradient.alphaKeys = new GradientAlphaKey[] {
new GradientAlphaKey(1f, 0f),
new GradientAlphaKey(0f, 1f)
};
var colorOverLifetime = particleSystem.colorOverLifetime;
colorOverLifetime.enabled = true;
colorOverLifetime.color = new ParticleSystem.MinMaxGradient(gradient);Sample Program
This script changes particles from white to blue and shrinks them to zero size as they live.
Unity
using UnityEngine; public class ColorSizeOverLifetime : MonoBehaviour { public ParticleSystem particleSystem; void Start() { var main = particleSystem.main; main.startColor = Color.white; main.startSize = 1f; var colorOverLifetime = particleSystem.colorOverLifetime; colorOverLifetime.enabled = true; Gradient gradient = new Gradient(); gradient.colorKeys = new GradientColorKey[] { new GradientColorKey(Color.white, 0f), new GradientColorKey(Color.blue, 1f) }; gradient.alphaKeys = new GradientAlphaKey[] { new GradientAlphaKey(1f, 0f), new GradientAlphaKey(0f, 1f) }; colorOverLifetime.color = new ParticleSystem.MinMaxGradient(gradient); var sizeOverLifetime = particleSystem.sizeOverLifetime; sizeOverLifetime.enabled = true; sizeOverLifetime.size = new ParticleSystem.MinMaxCurve(1f, AnimationCurve.Linear(0f, 1f, 1f, 0f)); } }
Important Notes
Always enable the colorOverLifetime and sizeOverLifetime modules to see changes.
Use gradients to control smooth color and transparency changes.
Animation curves let you customize size changes over time easily.
Summary
Color and size over lifetime make particles look natural and dynamic.
Enable and set gradients or curves to control these changes.
Use this to create effects like fading, growing, or color shifting.
Practice
1. What does the
ColorOverLifetime module do in Unity's Particle System?easy
Solution
Step 1: Understand the purpose of ColorOverLifetime
The ColorOverLifetime module is designed to modify particle colors gradually as they live.Step 2: Compare options with module function
Only It changes the color of particles smoothly as they age. correctly describes this smooth color change over particle lifetime.Final Answer:
It changes the color of particles smoothly as they age. -> Option BQuick Check:
ColorOverLifetime = Smooth color change [OK]
Hint: ColorOverLifetime changes colors gradually over particle life [OK]
Common Mistakes:
- Confusing ColorOverLifetime with initial color setting
- Thinking it controls size or speed
- Assuming it changes color instantly
2. Which of the following is the correct way to enable the SizeOverLifetime module in a Unity Particle System script?
easy
Solution
Step 1: Check correct property casing and spelling
Unity uses camelCase for properties, sosizeOverLifetimeis correct.Step 2: Verify property names and boolean flags
The property to enable isenabled, notenable.Final Answer:
particleSystem.sizeOverLifetime.enabled = true; -> Option CQuick Check:
Correct casing and property name = particleSystem.sizeOverLifetime.enabled = true; [OK]
Hint: Use camelCase and 'enabled' property to activate modules [OK]
Common Mistakes:
- Incorrect capitalization of 'sizeOverLifetime'
- Using 'enable' instead of 'enabled'
- Misspelling the module name
3. Given this code snippet in Unity:
var ps = GetComponent<ParticleSystem>(); var col = ps.colorOverLifetime; col.enabled = true; col.color = new ParticleSystem.MinMaxGradient(Color.red, Color.blue);What will happen to the particles' colors over their lifetime?
medium
Solution
Step 1: Analyze the colorOverLifetime setup
The code enables colorOverLifetime and sets a gradient from red to blue.Step 2: Understand gradient effect on particles
This gradient causes particles to smoothly transition from red at birth to blue at death.Final Answer:
Particles will smoothly change color from red to blue as they age. -> Option AQuick Check:
Gradient red to blue = smooth color change [OK]
Hint: Gradient from color A to B means smooth transition over lifetime [OK]
Common Mistakes:
- Assuming color stays constant
- Thinking color changes instantly
- Believing gradient is invalid without alpha
4. Identify the error in this Unity C# code for SizeOverLifetime:
var ps = GetComponent<ParticleSystem>(); var size = ps.sizeOverLifetime; size.enabled = true; size.size = new ParticleSystem.MinMaxCurve(1.0f, 0.0f);
medium
Solution
Step 1: Review MinMaxCurve usage for size
MinMaxCurve can define a linear change from start to end size; start larger than end is valid.Step 2: Confirm code logic
The code enables sizeOverLifetime and sets size to shrink from 1.0 to 0.0, which is correct.Final Answer:
There is no error; the code shrinks particles from size 1 to 0 correctly. -> Option DQuick Check:
Shrinking size from 1 to 0 is valid [OK]
Hint: Start size can be bigger than end size for shrinking effect [OK]
Common Mistakes:
- Thinking start value must be less than end value
- Confusing MinMaxCurve with animation curve
- Assuming reversed parameters cause error
5. You want particles to start fully transparent, become fully visible halfway through their life, then fade out again by the end. How can you set this using ColorOverLifetime in Unity?
hard
Solution
Step 1: Understand alpha control with ColorOverLifetime
Alpha keys in gradient let you control transparency at different life points.Step 2: Set gradient alpha keys for fade in and fade out
Alpha 0 at start, 1 at half life, and 0 at end creates the desired transparency effect.Final Answer:
Use a gradient with alpha keys at 0 (0), 0.5 (1), and 1 (0) to control transparency over lifetime. -> Option AQuick Check:
Alpha keys control transparency fade in/out [OK]
Hint: Use alpha keys in gradient for fade in and fade out [OK]
Common Mistakes:
- Only setting start and end alpha without middle key
- Using size to control transparency
- Changing emission rate instead of color alpha
