Bird
Raised Fist0
Unityframework~20 mins

Color and size over lifetime in Unity - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Particle Mastery: Color and Size Over Lifetime
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Unity particle color code?
Consider this Unity C# code snippet that changes a particle's color over its lifetime. What color will the particle have at half its lifetime?
Unity
var main = particleSystem.main;
main.startColor = new ParticleSystem.MinMaxGradient(
    new Gradient {
        colorKeys = new GradientColorKey[] {
            new GradientColorKey(Color.red, 0.0f),
            new GradientColorKey(Color.blue, 1.0f)
        },
        alphaKeys = new GradientAlphaKey[] {
            new GradientAlphaKey(1.0f, 0.0f),
            new GradientAlphaKey(1.0f, 1.0f)
        }
    }
);
// Particle lifetime is 1 second
AThe particle color will be purple (a mix of red and blue).
BThe particle color will be red.
CThe particle color will be blue.
DThe particle color will be green.
Attempts:
2 left
💡 Hint
Think about how gradients blend colors over time.
Predict Output
intermediate
2:00remaining
What size will the particle have at 75% of its lifetime?
Given this Unity C# code controlling particle size over lifetime, what is the particle size at 0.75 lifetime?
Unity
var sizeOverLifetime = particleSystem.sizeOverLifetime;
sizeOverLifetime.enabled = true;
sizeOverLifetime.size = new ParticleSystem.MinMaxCurve(1.0f, new AnimationCurve(
    new Keyframe(0.0f, 0.5f),
    new Keyframe(1.0f, 2.0f)
));
AThe particle size will be 1.0.
BThe particle size will be 0.5.
CThe particle size will be 2.0.
DThe particle size will be approximately 1.625.
Attempts:
2 left
💡 Hint
The size changes linearly from 0.5 to 2.0 over the lifetime.
🔧 Debug
advanced
2:00remaining
Why does this particle color over lifetime code cause an error?
This Unity C# code snippet tries to set a particle system's color over lifetime but causes a compile error. What is the cause?
Unity
var colorOverLifetime = particleSystem.colorOverLifetime;
colorOverLifetime.enabled = true;
colorOverLifetime.color = new Gradient();
colorOverLifetime.color.colorKeys = new GradientColorKey[] {
    new GradientColorKey(Color.green, 0.0f),
    new GradientColorKey(Color.yellow, 1.0f)
};
AGradient.colorKeys is a read-only property and cannot be assigned directly.
BGradientColorKey requires three parameters, but only two are provided.
CcolorOverLifetime.color must be assigned a MinMaxGradient, not a Gradient.
DThe particle system must be stopped before changing colorOverLifetime.
Attempts:
2 left
💡 Hint
Check if you can assign to colorKeys directly.
📝 Syntax
advanced
2:00remaining
Which option correctly sets size over lifetime with a curve in Unity C#?
Choose the correct code snippet that sets a particle system's size over lifetime using an AnimationCurve.
AsizeOverLifetime.size = new AnimationCurve(new Keyframe(0, 1), new Keyframe(1, 0));
BsizeOverLifetime.size = new ParticleSystem.MinMaxCurve(1.0f, new AnimationCurve(new Keyframe(0, 1), new Keyframe(1, 0)));
CsizeOverLifetime.size = new ParticleSystem.MinMaxCurve(new AnimationCurve(new Keyframe(0, 1), new Keyframe(1, 0)));
DsizeOverLifetime.size = new ParticleSystem.MinMaxCurve(1.0f, 1.0f);
Attempts:
2 left
💡 Hint
MinMaxCurve constructor can take a multiplier and an AnimationCurve.
🚀 Application
expert
3:00remaining
How to create a particle system that fades color from white to transparent and grows in size over lifetime?
You want a Unity particle system where particles start white and fade to transparent, while their size grows from 0.1 to 1.0 over their lifetime. Which code snippet achieves this?
A
var col = ps.colorOverLifetime;
col.enabled = true;
var grad = new Gradient();
grad.SetKeys(
    new GradientColorKey[] { new GradientColorKey(Color.white, 0f), new GradientColorKey(Color.white, 1f) },
    new GradientAlphaKey[] { new GradientAlphaKey(1f, 0f), new GradientAlphaKey(0f, 1f) }
);
col.color = new ParticleSystem.MinMaxGradient(grad);
var size = ps.sizeOverLifetime;
size.enabled = true;
size.size = new ParticleSystem.MinMaxCurve(1f, new AnimationCurve(new Keyframe(0f, 0.1f), new Keyframe(1f, 1f)));
B
var col = ps.colorOverLifetime;
col.enabled = true;
col.color = new ParticleSystem.MinMaxGradient(Color.white);
var size = ps.sizeOverLifetime;
size.enabled = true;
size.size = new ParticleSystem.MinMaxCurve(0.1f, 1f);
C
var col = ps.colorOverLifetime;
col.enabled = true;
var grad = new Gradient();
grad.SetKeys(
    new GradientColorKey[] { new GradientColorKey(Color.white, 0f), new GradientColorKey(Color.clear, 1f) },
    new GradientAlphaKey[] { new GradientAlphaKey(1f, 0f), new GradientAlphaKey(0f, 1f) }
);
col.color = new ParticleSystem.MinMaxGradient(grad);
var size = ps.sizeOverLifetime;
size.enabled = true;
size.size = new ParticleSystem.MinMaxCurve(1f, new AnimationCurve(new Keyframe(0f, 0.1f), new Keyframe(1f, 1f)));
D
var col = ps.colorOverLifetime;
col.enabled = true;
col.color = new ParticleSystem.MinMaxGradient(Color.white);
var size = ps.sizeOverLifetime;
size.enabled = true;
size.size = new AnimationCurve(new Keyframe(0f, 0.1f), new Keyframe(1f, 1f));
Attempts:
2 left
💡 Hint
Use GradientColorKey with Color.clear for transparency and set size with MinMaxCurve and AnimationCurve.

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