0
0
Unityframework~20 mins

UI animations in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
UI Animation Master
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 UI animation code?

Consider this Unity C# script snippet that animates a UI element's opacity over time using a coroutine. What will be the final alpha value of the UI element's CanvasGroup after the coroutine finishes?

Unity
using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class FadeUI : MonoBehaviour
{
    public CanvasGroup canvasGroup;

    IEnumerator FadeOut()
    {
        float duration = 2f;
        float elapsed = 0f;
        while (elapsed < duration)
        {
            canvasGroup.alpha = Mathf.Lerp(1f, 0f, elapsed / duration);
            elapsed += Time.deltaTime;
            yield return null;
        }
        canvasGroup.alpha = 0f;
    }

    void Start()
    {
        StartCoroutine(FadeOut());
    }
}
AThe alpha value will be 0.5 after 2 seconds, making the UI element semi-transparent.
BThe alpha value will be 1 after 2 seconds, so the UI element remains fully visible.
CThe alpha value will be 0 after 2 seconds, making the UI element fully transparent.
DThe code will cause a runtime error because canvasGroup is not assigned.
Attempts:
2 left
💡 Hint

Look at the Mathf.Lerp function and the final assignment after the loop.

🧠 Conceptual
intermediate
1:30remaining
Which Unity component is best for animating UI element position smoothly?

You want to move a UI button smoothly from one position to another over time. Which Unity component or method is best suited for this animation?

AUse <code>RectTransform</code> and animate its <code>anchoredPosition</code> property with a coroutine or tweening library.
BUse <code>Transform.position</code> directly to move the UI element in world space.
CUse <code>CanvasGroup.alpha</code> to animate the position by changing transparency.
DUse <code>Animator</code> component only for 3D models, not UI elements.
Attempts:
2 left
💡 Hint

UI elements use RectTransform for layout and positioning inside the Canvas.

🔧 Debug
advanced
2:30remaining
Why does this UI animation script freeze the game?

Look at this Unity C# code snippet intended to animate a UI element's scale. Why does the game freeze when this code runs?

Unity
using UnityEngine;

public class ScaleUI : MonoBehaviour
{
    public RectTransform uiElement;

    void Start()
    {
        while (uiElement.localScale.x < 2f)
        {
            uiElement.localScale += new Vector3(0.01f, 0.01f, 0);
        }
    }
}
AThe RectTransform component is missing, so uiElement is null and causes a NullReferenceException.
BThe code tries to add a Vector3 to localScale which is not allowed, causing a compile error.
CThe scale increases too fast and causes an overflow error.
DThe while loop runs in Start() without yielding, blocking the main thread and freezing the game.
Attempts:
2 left
💡 Hint

Think about how Unity's main thread and frame updates work with loops.

📝 Syntax
advanced
2:00remaining
Which option correctly animates UI element's color using Unity's Update method?

Choose the code snippet that correctly changes a UI Image's color from white to red over 3 seconds inside Update().

A
float t = 0f;
void Update() {
  t += Time.deltaTime / 3f;
  GetComponent&lt;Image&gt;().color = Color.Lerp(Color.white, Color.red, t);
}
B
void Update() {
  float t = 0f;
  t += Time.deltaTime / 3f;
  GetComponent&lt;Image&gt;().color = Color.Lerp(Color.white, Color.red, t);
}
C
float t = 0f;
void Update() {
  t += Time.deltaTime * 3f;
  GetComponent&lt;Image&gt;().color = Color.Lerp(Color.white, Color.red, t);
}
D
float t = 0f;
void Update() {
  t += Time.deltaTime / 3f;
  GetComponent&lt;Image&gt;().color = Color.Lerp(Color.red, Color.white, t);
}
Attempts:
2 left
💡 Hint

Remember to declare t outside Update() to keep its value between frames.

🚀 Application
expert
3:00remaining
How many keyframes are created by this Unity AnimationClip code?

This code creates an AnimationClip that animates a UI element's scale from 1 to 2 over 1 second with 4 keyframes. How many keyframes does the clip actually contain?

Unity
using UnityEngine;

public class CreateClip : MonoBehaviour
{
    void Start()
    {
        AnimationClip clip = new AnimationClip();
        Keyframe[] keysX = new Keyframe[] {
            new Keyframe(0f, 1f),
            new Keyframe(0.33f, 1.5f),
            new Keyframe(0.66f, 1.75f),
            new Keyframe(1f, 2f)
        };
        AnimationCurve curveX = new AnimationCurve(keysX);
        clip.SetCurve("", typeof(RectTransform), "m_LocalScale.x", curveX);
    }
}
A3 keyframes, because Unity merges close keyframes automatically.
B4 keyframes, exactly as defined in the keysX array.
C1 keyframe, since only the last keyframe is used in the animation.
D0 keyframes, because the clip is not assigned to any Animator.
Attempts:
2 left
💡 Hint

Think about how AnimationCurve stores keyframes and if Unity modifies them automatically.