A singleton is a special class that can only have one instance. Because of this, it’s super-easy to reference functions inside a singleton from another class. Singletons can talk to other singletons, and non-singleton classes can talk to singletons, but singletons can not talk to non-singleton classes.
Here’s an example were we use singletons to send debug messages, change a text box, and instantiate a prefab:
Without using singletons, we might try and access a ui component from a player script like this:
Ok, let’s build a Finite State Machine that will act as our smart AI. As our AI travels the waypoints, we want it to go into attack mode at each point. For now we’ll just have it stop for 3 seconds and then move on to the next waypoint. Like this:
All we need is an If statement in our Attack Switch Case that calls a coroutine and updates the switch: