Enums in the Inspector
Part of the Game Architecture in Unity section of Coddy's Unity journey. Lesson 20 of 64.
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
EasyLet a door need several keycards and the ring hold several.
- In
KeyRing.cs, put[System.Flags]aboveAccessand number itNone = 0, Red = 1, Blue = 2, Green = 4. Add(Access card)keeps the cards already there:cards |= card;.Opens(Access needs)returns true only when the ring holds every card inneeds.
Press Play and pick up both cards: the Vault opens. 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;
}
}
All lessons in Game Architecture in Unity
1Components, Not Hierarchies
Small ComponentsA Base Enemy ScriptAbstract ComponentsComponents Working TogetherRecap: Modular TurretRecap: Twin Cannon4Serialized Data
Serializable ClassesLists in the InspectorEnums in the InspectorRecap: Loot TableRecap: Roll the Loot2Interfaces in Unity
IDamageableInterfaces and TryGetComponentGetComponentsRecap: Interact KeyRecap: Nearest in ReachPractice on your own: Unity playground