Consider this simple AI state machine snippet in Unity C#. What will be printed when the AI transitions from Idle to Chase state?
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}"); } }
Look at the order of Debug.Log calls and the initial value of currentState.
The AI starts in Idle state and prints it. Then it changes to Chase and prints the new state.
Given an AI with states Patrol, Alert, and Attack, which transition is typically valid in a state machine controlling AI behavior?
Think about how AI usually detects threats and reacts.
AI usually patrols, then becomes alert when it senses something, then attacks if threat confirmed.
Examine this Unity C# code snippet for an AI state machine. Why does it cause a compile error?
using UnityEngine;
public class AIStateMachine : MonoBehaviour {
enum State { Idle, Chase }
private State currentState;
void Start() {
Debug.Log(currentState.ToString());
}
}Check the declaration order of the enum State and currentState field.
The enum State is declared after the private State currentState; field. In C#, types must be declared before they are used in field declarations within a class, causing a compile error (CS0246: The type or namespace name 'State' could not be found).
Choose the correct method to change the AI state from Idle to Chase safely.
enum State { Idle, Chase }
State currentState = State.Idle;Remember how to assign values in C# and how to compare values.
Option A correctly assigns newState to currentState. Option A uses comparison operator == instead of assignment =. Option A uses a method equals which is invalid for enums. Option A tries to call newState as a method.
Given this Unity C# code for an AI state machine, how many states are stored in the states dictionary after InitializeStates() runs?
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); } }
Remember how dictionary keys work when you assign a value to an existing key.
The dictionary has 4 keys from the enum. The last assignment updates the value for State.Patrol but does not add a new key. So the count remains 4.