0
0
Unityframework~20 mins

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

Choose your learning style9 modes available
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.