How does it work?

6_ScriptableEnums

ScriptableEnums are simply ScriptableObjects used as enums. To compare them, we just compare their references. If you open the Element.cs component located on an Element object, you can see the code.

[SelectionBase]
public class Element : MonoBehaviour
{
    [SerializeField] private ScriptableEnumElement _elementType = null;
    private ScriptableEnumElement ElementType => _elementType;

    private void Start()
    {
        GetComponent<Renderer>().material.color = _elementType.Color;
        GetComponentInChildren<TextMeshPro>().text = _elementType.Name;
    }

    private void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.TryGetComponent<Element>(out var element))
        {
            if (_elementType.Defeats.Contains(element.ElementType))
                Destroy(other.gameObject);
        }
    }
}

In our case, when an element collides with another, we check if the other element type is contained in our Defeats list. If it is, then we can destroy it. This approach is flexible because you can add or remove element types from this list at runtime or through code. Additionally, if you want to create a new element, all you need to do is duplicate an existing SE and rename it. That’s it—no code involved.

Last updated