Bird
Raised Fist0
Unityframework~10 mins

Color and size over lifetime in Unity - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Color and size over lifetime
Start Particle
Initialize Color & Size
Update Lifetime
Calculate Color & Size based on Lifetime
Apply Color & Size
Check if Lifetime Ended?
NoLoop Update
Yes
Destroy Particle
This flow shows how a particle's color and size change as its lifetime progresses until it is destroyed.
Execution Sample
Unity
void Update()
{
  float lifeRatio = elapsedTime / totalLifetime;
  color = Color.Lerp(startColor, endColor, lifeRatio);
  size = Mathf.Lerp(startSize, endSize, lifeRatio);
}
This code updates the particle's color and size smoothly over its lifetime.
Execution Table
StepelapsedTime (s)lifeRatioColor (R,G,B,A)SizeAction
10.00.0(1,1,1,1)1.0Initialize particle with startColor and startSize
20.50.1(0.9,0.9,0.9,0.9)1.1Update color and size slightly
31.00.2(0.8,0.8,0.8,0.8)1.2Update color and size
42.50.5(0.5,0.5,0.5,0.5)1.5Mid lifetime color and size
54.00.8(0.2,0.2,0.2,0.2)1.8Near end color and size
65.01.0(0,0,0,0)2.0Lifetime ended, final color and size
75.01.0(0,0,0,0)2.0Destroy particle
💡 Particle lifetime reached 5.0 seconds, condition to destroy particle met.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
elapsedTime0.00.51.02.54.05.05.0
lifeRatio0.00.10.20.50.81.01.0
color(1,1,1,1)(0.9,0.9,0.9,0.9)(0.8,0.8,0.8,0.8)(0.5,0.5,0.5,0.5)(0.2,0.2,0.2,0.2)(0,0,0,0)(0,0,0,0)
size1.01.11.21.51.82.02.0
Key Moments - 3 Insights
Why does the color and size change smoothly instead of jumping suddenly?
Because the code uses interpolation (Lerp) based on lifeRatio, which gradually changes from 0 to 1 as elapsedTime increases (see execution_table rows 2 to 6).
What happens when elapsedTime equals totalLifetime?
The particle reaches the end of its lifetime, color and size reach their final values, and the particle is destroyed (see execution_table rows 6 and 7).
Why is lifeRatio calculated as elapsedTime divided by totalLifetime?
This normalizes elapsedTime to a 0-1 range, making it easy to interpolate color and size proportionally over the particle's lifetime (see variable_tracker lifeRatio values).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the color at step 4?
A(0.8,0.8,0.8,0.8)
B(0.2,0.2,0.2,0.2)
C(0.5,0.5,0.5,0.5)
D(1,1,1,1)
💡 Hint
Check the Color column at step 4 in the execution_table.
At which step does the size first reach 1.5?
AStep 4
BStep 3
CStep 5
DStep 6
💡 Hint
Look at the Size column in the execution_table and find when size is 1.5.
If totalLifetime was increased to 10 seconds, how would lifeRatio at step 4 change?
AIt would be 1.0
BIt would be 0.25
CIt would be 0.5
DIt would be 0.8
💡 Hint
lifeRatio = elapsedTime / totalLifetime; step 4 elapsedTime is 2.5 seconds.
Concept Snapshot
Color and size over lifetime in Unity:
- Use elapsedTime / totalLifetime to get lifeRatio (0 to 1).
- Use Color.Lerp(startColor, endColor, lifeRatio) to change color smoothly.
- Use Mathf.Lerp(startSize, endSize, lifeRatio) to change size smoothly.
- Update these every frame until lifetime ends.
- Destroy particle when lifetime is over.
Full Transcript
This visual execution shows how a particle's color and size change smoothly over its lifetime in Unity. The key is calculating lifeRatio as elapsedTime divided by totalLifetime, which goes from 0 to 1. Using this ratio, the code interpolates color and size from start to end values using Lerp functions. Each step updates elapsedTime, recalculates lifeRatio, and applies new color and size. When elapsedTime reaches totalLifetime, the particle is destroyed. This approach ensures smooth transitions and proper lifetime management.

Practice

(1/5)
1. What does the ColorOverLifetime module do in Unity's Particle System?
easy
A. It adjusts the size of particles instantly.
B. It changes the color of particles smoothly as they age.
C. It controls the speed of particles over time.
D. It sets the initial color of particles only.

Solution

  1. Step 1: Understand the purpose of ColorOverLifetime

    The ColorOverLifetime module is designed to modify particle colors gradually as they live.
  2. 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.
  3. Final Answer:

    It changes the color of particles smoothly as they age. -> Option B
  4. Quick 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
A. particleSystem.sizeoverlifetime.enabled = true;
B. particleSystem.SizeOverLifetime.enabled = true;
C. particleSystem.sizeOverLifetime.enabled = true;
D. particleSystem.sizeOverLifetime.enable = true;

Solution

  1. Step 1: Check correct property casing and spelling

    Unity uses camelCase for properties, so sizeOverLifetime is correct.
  2. Step 2: Verify property names and boolean flags

    The property to enable is enabled, not enable.
  3. Final Answer:

    particleSystem.sizeOverLifetime.enabled = true; -> Option C
  4. Quick 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
A. Particles will smoothly change color from red to blue as they age.
B. Particles will stay red throughout their life.
C. Particles will instantly switch from red to blue at half their lifetime.
D. Particles will not change color because the gradient is invalid.

Solution

  1. Step 1: Analyze the colorOverLifetime setup

    The code enables colorOverLifetime and sets a gradient from red to blue.
  2. Step 2: Understand gradient effect on particles

    This gradient causes particles to smoothly transition from red at birth to blue at death.
  3. Final Answer:

    Particles will smoothly change color from red to blue as they age. -> Option A
  4. Quick 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
A. MinMaxCurve cannot have start value greater than end value.
B. The size curve should be assigned using a curve, not MinMaxCurve.
C. The MinMaxCurve constructor parameters are reversed; it should be (0.0f, 1.0f).
D. There is no error; the code shrinks particles from size 1 to 0 correctly.

Solution

  1. Step 1: Review MinMaxCurve usage for size

    MinMaxCurve can define a linear change from start to end size; start larger than end is valid.
  2. Step 2: Confirm code logic

    The code enables sizeOverLifetime and sets size to shrink from 1.0 to 0.0, which is correct.
  3. Final Answer:

    There is no error; the code shrinks particles from size 1 to 0 correctly. -> Option D
  4. Quick 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
A. Use a gradient with alpha keys at 0 (0), 0.5 (1), and 1 (0) to control transparency over lifetime.
B. Set the start color alpha to 0 and end color alpha to 1 in ColorOverLifetime gradient.
C. Enable SizeOverLifetime and animate size from 0 to 1 to 0 to control visibility.
D. Use a single color with alpha 1 and animate particle emission rate.

Solution

  1. Step 1: Understand alpha control with ColorOverLifetime

    Alpha keys in gradient let you control transparency at different life points.
  2. 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.
  3. Final Answer:

    Use a gradient with alpha keys at 0 (0), 0.5 (1), and 1 (0) to control transparency over lifetime. -> Option A
  4. Quick 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