0
0
Unityframework~20 mins

State machines for AI behavior in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
AI State Machine 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 C# state machine code?

Consider this simple AI state machine snippet in Unity C#. What will be printed when the AI transitions from Idle to Chase state?

Unity
using UnityEngine;

public class AIStateMachine : MonoBehaviour {
    enum State { Idle, Chase }
    State currentState = State.Idle;

    void Start() {
        Debug.Log($"Starting state: {currentState}");
        currentState = State.Chase;
        Debug.Log($"Changed state to: {currentState}");
    }
}
AStarting state: Idle
BStarting state: Chase\nChanged state to: Idle
CStarting state: Idle\nChanged state to: Chase
DChanged state to: Chase
Attempts:
2 left
💡 Hint

Look at the order of Debug.Log calls and the initial value of currentState.

🧠 Conceptual
intermediate
1:30remaining
Which state transition is valid in this AI state machine?

Given an AI with states Patrol, Alert, and Attack, which transition is typically valid in a state machine controlling AI behavior?

AAttack → Alert → Patrol
BPatrol → Alert → Attack
CAlert → Patrol → Attack
DAttack → Patrol → Alert
Attempts:
2 left
💡 Hint

Think about how AI usually detects threats and reacts.

🔧 Debug
advanced
2:30remaining
Why does this AI state machine code cause a compile error?

Examine this Unity C# code snippet for an AI state machine. Why does it cause a compile error?

Unity
using UnityEngine;

public class AIStateMachine : MonoBehaviour {
    enum State { Idle, Chase }

    private State currentState;

    void Start() {
        Debug.Log(currentState.ToString());
    }
}
AEnum State is declared after usage causing compile error.
BcurrentState is not initialized before use, so it is null causing NullReferenceException.
CMonoBehaviour cannot have private fields.
DDebug.Log cannot print enum values.
Attempts:
2 left
💡 Hint

Check the declaration order of the enum State and currentState field.

📝 Syntax
advanced
1:30remaining
Which option correctly implements a state transition method in Unity C#?

Choose the correct method to change the AI state from Idle to Chase safely.

Unity
enum State { Idle, Chase }
State currentState = State.Idle;
Avoid ChangeState(State newState) { currentState = newState; }
Bvoid ChangeState(State newState) { currentState = newState(); }
Cvoid ChangeState(State newState) { currentState.equals(newState); }
Dvoid ChangeState(State newState) { if(newState != currentState) currentState == newState; }
Attempts:
2 left
💡 Hint

Remember how to assign values in C# and how to compare values.

🚀 Application
expert
3:00remaining
How many states does this AI state machine have after execution?

Given this Unity C# code for an AI state machine, how many states are stored in the states dictionary after InitializeStates() runs?

Unity
using System.Collections.Generic;
using UnityEngine;

public class AIStateMachine : MonoBehaviour {
    enum State { Idle, Patrol, Chase, Attack }

    Dictionary<State, string> states = new Dictionary<State, string>();

    void InitializeStates() {
        states[State.Idle] = "Idle behavior";
        states[State.Patrol] = "Patrol behavior";
        states[State.Chase] = "Chase behavior";
        states[State.Attack] = "Attack behavior";
        states[State.Patrol] = "Updated Patrol behavior";
    }

    void Start() {
        InitializeStates();
        Debug.Log(states.Count);
    }
}
A3
B5
C1
D4
Attempts:
2 left
💡 Hint

Remember how dictionary keys work when you assign a value to an existing key.