Menu
CoddyTech

Recap: Key and Door

Part of the Physics and Gameplay section of Coddy's Unity journey. Lesson 21 of 71.

This recap puts the chapter together: triggers, OnTriggerEnter, and TryGetComponent to make sure it is the player who came in.

challenge icon

Challenge

Easy

Write the key and the door: only the player can use them.

  1. In Key.cs's OnTriggerEnter, when other.TryGetComponent(out Player player), set player.hasKey = true;, log Got the key and destroy the key.
  2. In Door.cs, for the player: with hasKey, gate.SetActive(false); and log The door opens; otherwise log Locked.

Press Play, try the door, fetch the key and try again. Then Check.

Try it yourself

using UnityEngine;

public class Key : MonoBehaviour
{
    // The player takes the key; nothing else does
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Physics and Gameplay

Practice on your own: Unity playground