Bird
Raised Fist0
Unityframework~20 mins

Why AI makes games challenging in Unity - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
AI Game Challenge 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# AI decision code?
Consider this simple AI decision code snippet in Unity C#. What will be printed when the AI's health is 30?
Unity
int health = 30;
string action;
switch (health) {
    case int h when (h > 50):
        action = "Attack";
        break;
    case int h when (h > 20):
        action = "Defend";
        break;
    default:
        action = "Retreat";
        break;
}
Debug.Log(action);
AAttack
BNo output (runtime error)
CRetreat
DDefend
Attempts:
2 left
💡 Hint
Think about which case matches when health is 30.
🧠 Conceptual
intermediate
1:30remaining
Why does AI unpredictability increase game challenge?
Which of these best explains why AI unpredictability makes games more challenging?
ABecause AI unpredictability makes the game graphics better.
BBecause predictable AI allows players to easily plan and win.
CBecause unpredictable AI always cheats to win.
DBecause AI unpredictability reduces game difficulty.
Attempts:
2 left
💡 Hint
Think about how players react to patterns.
🔧 Debug
advanced
2:30remaining
Find the error causing AI to always attack in Unity C#
This AI code is supposed to choose between attacking or retreating based on health, but it causes a compile error. What is the bug?
Unity
int health = 10;
string action;
if (health > 20)
    action = "Attack";
else if (health < 20)
    action = "Retreat";
Debug.Log(action);
AThe code is missing braces {} around if and else if blocks.
BThe variable 'action' is not initialized before use.
CThe else if condition should be 'health <= 20' to cover all cases.
DThe Debug.Log statement is outside the if-else blocks.
Attempts:
2 left
💡 Hint
Check if all health values are covered by conditions.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in Unity C# AI code?
Which of these AI code snippets will cause a syntax error when compiled?
Aif enemyDistance < 5 { action = "Attack"; } else { action = "Retreat"; }
Bif (enemyDistance < 5) { action = "Attack"; } else { action = "Retreat"; }
Caction = enemyDistance < 5 ? "Attack" : "Retreat";
Dswitch (enemyDistance) { case < 5: action = "Attack"; break; default: action = "Retreat"; break; }
Attempts:
2 left
💡 Hint
Check the syntax of the if statement.
🚀 Application
expert
3:00remaining
How many unique actions can this AI perform?
Given this Unity C# AI code, how many unique actions can the AI perform?
Unity
int health = 40;
int enemyDistance = 10;
string action;
if (health > 50 && enemyDistance < 5) {
    action = "Attack";
} else if (health > 20 || enemyDistance < 10) {
    action = "Defend";
} else {
    action = "Retreat";
}
Debug.Log(action);
A3
B1
C4
D2
Attempts:
2 left
💡 Hint
Count all possible distinct actions from the conditions.

Practice

(1/5)
1. Why does AI make games more challenging for players?
easy
A. Because AI controls characters that react and adapt to player actions
B. Because AI makes the game graphics better
C. Because AI reduces the number of levels in the game
D. Because AI slows down the game speed

Solution

  1. Step 1: Understand AI role in games

    AI controls game characters to behave smartly, reacting to what players do.
  2. Step 2: Identify how this affects challenge

    Smart reactions make the game less predictable and more difficult.
  3. Final Answer:

    Because AI controls characters that react and adapt to player actions -> Option A
  4. Quick Check:

    AI reaction = challenge [OK]
Hint: AI makes characters smart and reactive to players [OK]
Common Mistakes:
  • Thinking AI improves graphics
  • Believing AI reduces game levels
  • Assuming AI slows game speed
2. Which of the following is the correct way to check a condition in Unity C# AI script?
easy
A. if playerHealth < 50 then Attack();
B. if (playerHealth < 50) Attack(); else
C. if playerHealth < 50 { Attack(); }
D. if (playerHealth < 50) { Attack(); }

Solution

  1. Step 1: Recall C# syntax for if statements

    In C#, conditions use parentheses and braces: if (condition) { code }
  2. Step 2: Compare options to correct syntax

    if (playerHealth < 50) { Attack(); } matches correct syntax; others have syntax errors.
  3. Final Answer:

    if (playerHealth < 50) { Attack(); } -> Option D
  4. Quick Check:

    C# if syntax = if (condition) { } [OK]
Hint: Use parentheses and braces for if in C# [OK]
Common Mistakes:
  • Omitting parentheses around condition
  • Using 'then' keyword (not in C#)
  • Leaving else without code block
3. What will be the output of this Unity C# code snippet controlling AI behavior?
int playerDistance = 10;
if (playerDistance < 5) {
    Debug.Log("Attack");
} else {
    Debug.Log("Chase");
}
medium
A. Chase
B. Error
C. No output
D. Attack

Solution

  1. Step 1: Check the condition playerDistance < 5

    playerDistance is 10, which is not less than 5, so condition is false.
  2. Step 2: Determine which branch runs

    Since condition is false, the else block runs, printing "Chase".
  3. Final Answer:

    Chase -> Option A
  4. Quick Check:

    10 < 5? No, so output = Chase [OK]
Hint: Check condition truth to pick output branch [OK]
Common Mistakes:
  • Assuming condition is true without checking value
  • Confusing else and if blocks
  • Expecting no output when else runs
4. Identify the error in this Unity C# AI script snippet:
if playerVisible == true
{
    Attack();
}
medium
A. Using '==' instead of '=' for assignment
B. Missing semicolon after Attack()
C. Missing parentheses around the if condition
D. Braces should be parentheses

Solution

  1. Step 1: Check if statement syntax in C#

    C# requires parentheses around the condition in if statements.
  2. Step 2: Identify the error in the code

    The code lacks parentheses around 'playerVisible == true', causing syntax error.
  3. Final Answer:

    Missing parentheses around the if condition -> Option C
  4. Quick Check:

    if condition needs ( ) in C# [OK]
Hint: Always put conditions inside parentheses in if [OK]
Common Mistakes:
  • Forgetting parentheses in if statements
  • Confusing '==' with '=' for comparison
  • Misplacing braces
5. You want an AI enemy in Unity to choose between attacking or running away based on its health. Which code snippet correctly implements this logic?
hard
A. if enemyHealth > 30 then Attack() else RunAway()
B. if (enemyHealth > 30) { Attack(); } else { RunAway(); }
C. if (enemyHealth < 30) { Attack(); } else { RunAway(); }
D. if (enemyHealth = 30) { Attack(); } else { RunAway(); }

Solution

  1. Step 1: Understand the logic needed

    If enemy health is high (greater than 30), it should attack; otherwise, run away.
  2. Step 2: Check each option's condition and syntax

    if (enemyHealth > 30) { Attack(); } else { RunAway(); } correctly checks if health is greater than 30 and uses proper C# syntax.
  3. Final Answer:

    if (enemyHealth > 30) { Attack(); } else { RunAway(); } -> Option B
  4. Quick Check:

    Health > 30 means attack, else run [OK]
Hint: Use if (condition) { } else { } with correct comparison [OK]
Common Mistakes:
  • Using wrong comparison operators
  • Missing parentheses or braces
  • Using assignment '=' instead of comparison '=='
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard