0
0
Unityframework~20 mins

MonoBehaviour lifecycle in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MonoBehaviour Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output order of these MonoBehaviour methods?

Consider a Unity script attached to an active GameObject. Which sequence of method calls will Unity invoke first when the scene starts?

Unity
using UnityEngine;

public class TestLifecycle : MonoBehaviour
{
    void Awake() { Debug.Log("Awake"); }
    void Start() { Debug.Log("Start"); }
    void OnEnable() { Debug.Log("OnEnable"); }
}
AStart, Awake, OnEnable
BOnEnable, Awake, Start
CAwake, OnEnable, Start
DAwake, Start, OnEnable
Attempts:
2 left
💡 Hint

Remember that Awake is called before OnEnable, and Start is called after both.

Predict Output
intermediate
2:00remaining
What happens if you disable a GameObject during Update?

Given this script, what will be the output when the GameObject is active and Update disables it?

Unity
using UnityEngine;

public class DisableInUpdate : MonoBehaviour
{
    void Update()
    {
        Debug.Log("Update called");
        gameObject.SetActive(false);
    }

    void OnDisable()
    {
        Debug.Log("OnDisable called");
    }
}
AOnly OnDisable called
BUpdate called\nOnDisable called
COnly Update called
DOnDisable called\nUpdate called
Attempts:
2 left
💡 Hint

Think about the order of method calls within the same frame.

🔧 Debug
advanced
2:00remaining
Why does Start not get called when re-enabling a previously active GameObject?

Look at this script attached to a GameObject that starts active (so Start has already been called), is then disabled, and re-enabled at runtime. Why does Start not print "Start called" again?

Unity
using UnityEngine;

public class StartTest : MonoBehaviour
{
    void Start()
    {
        Debug.Log("Start called");
    }
}

// Elsewhere in code:
// gameObject.SetActive(false);
// gameObject.SetActive(true);
AStart is only called once when the script instance is first enabled, not every time the GameObject is enabled.
BStart is called only if Awake is called first, which is missing here.
CStart is called only if OnEnable is overridden in the script.
DStart is called only if the GameObject is enabled in the editor before play.
Attempts:
2 left
💡 Hint

Think about when Start is triggered in the lifecycle.

📝 Syntax
advanced
2:00remaining
Which method signature is correct for OnDestroy in MonoBehaviour?

Choose the correct method signature for the OnDestroy callback in a MonoBehaviour script.

Avoid OnDestroy()
Bvoid OnDestroy(bool isPermanent)
Cpublic void OnDestroy()
Dvoid OnDestroy(GameObject obj)
Attempts:
2 left
💡 Hint

Check the Unity documentation for OnDestroy method signature.

🚀 Application
expert
2:00remaining
How many times is FixedUpdate called per second if Time.fixedDeltaTime = 0.02?

In Unity, if Time.fixedDeltaTime is set to 0.02 seconds, how many times will FixedUpdate be called per second?

A20 times
BDepends on frame rate
C100 times
D50 times
Attempts:
2 left
💡 Hint

Think about how fixedDeltaTime relates to FixedUpdate frequency.