Enums in the Inspector
Part of the Game Architecture in Unity section of Coddy's Unity journey. Lesson 17 of 50.
An enum field shows in the Inspector as a dropdown, which makes it a handy way to set up objects that share one script. One Pickup prefab serves for coins, hearts and shields: each copy in the level gets its kind from the dropdown, and the script switches on it:
public enum PickupKind { Coin, Heart, Shield }
public class Pickup : MonoBehaviour
{
public PickupKind kind;
public int amount = 1;
void OnTriggerEnter(Collider other)
{
if (!other.TryGetComponent(out PlayerStats stats)) return;
switch (kind)
{
case PickupKind.Coin: stats.coins += amount; break;
case PickupKind.Heart: stats.hp += amount; break;
case PickupKind.Shield: stats.shielded = true; break;
}
Destroy(gameObject);
}
}The scene saves each choice as the value's number: Coin is 0, Heart 1, Shield 2. Adding a value at the end is safe. Inserting one at the front, as in { Gem, Coin, Heart, Shield }, shifts every number, and every pickup in every scene changes kind. Renaming a value is safe, since its number stays the same. Writing the numbers out, Coin = 0, Heart = 1, Shield = 2, lets you reorder the names later without moving any saved choice.
A [System.Flags] enum holds several values at once. Its dropdown has a tick per value, plus Nothing and Everything. Each value needs a bit of its own, so the numbers are powers of two:
[System.Flags]
public enum Element { None = 0, Fire = 1, Ice = 2, Poison = 4 }
public Element immuneTo; // ticked in the Inspector: Fire and Poison
public void Hit(int damage, Element element)
{
if (immuneTo.HasFlag(element)) return;
hp -= damage;
}| combines values: Element.Fire | Element.Poison is 5. HasFlag(x) is true when every value in x is set, and ToString() lists the values that are set, as Fire, Poison.
Challenge
EasyThe base has three doors: the Storeroom needs the red keycard, the Lab the blue one, and the Vault both. Each door's needs and each card's card are set in the Inspector with the Access enum, and Door.cs and Keycard.cs are ready. The arrows walk the Player, whose KeyRing holds the cards picked up. Finish KeyRing.cs:
- Make
Accessan enum whose values can be combined, so a door can need several cards and the ring can hold several. Keep its four names: None, Red, Blue and Green. Add(Access card)puts a card on the ring, keeping the cards already there.Opens(Access needs)returns true when the ring holds every card inneeds.
Press Play, try the doors, pick up the cards, then Check.
Try it yourself
using UnityEngine;
public enum Access { None, Red, Blue, Green }
public class KeyRing : MonoBehaviour
{
public Access cards;
public void Add(Access card)
{
// put the card on the ring, keeping the cards already there
}
public bool Opens(Access needs)
{
// true when the ring holds every card in needs
return false;
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Game Architecture in Unity
1Components, Not Hierarchies
Small ComponentsA Base Enemy ScriptAbstract ComponentsComponents Working TogetherRecap: Modular Turret10Project: Arena Survivor
The HeroEnemy Types