I am attempting to find out the perfect architectural strategy for dealing with communication in Unity, particularly when the interplay is strictly one-to-one.
The Core Design Battle
I’ve 5 essential, scene-persistent companies (e.g., AudioManager, SaveManager) that must be accessed by almost each different object within the scene.
My present strategy is to implement these companies as Singletons to permit for direct entry (e.g., AudioManager.Occasion.PlaySfx()).
Furthermore, I’ve a (not singleton) gameobject (let’s name it X) which has only a 1-to-1 communication with one other (not singleton) gameobject (let’s name it Y). On this case, I simply assign a reference from the inspector. Y can be the one receiver of the occasion, so why not simply doing one thing like yObject.foo(); inside X class?
Nonetheless, some builders closely advocate for utilizing a decoupled Occasion System even for single-listener situations (e.g., elevating an OnPlaySfx occasion that solely the AudioManager subscribes to).
My place is that utilizing occasions for one-to-one communication is pointless architectural overkill. Why introduce the complexity of an occasion system when a easy, direct methodology name on a Singleton (or gameobject on the whole) is cleaner and extra specific?
Here is the ScriptableObject I take advantage of to create occasions (this was given me by my professor, saying that he discovered it from a Unity e-book):
[CreateAssetMenu(menuName = "Events/Void Event Channel")]
public class VoidEventChannelSO : ScriptableObject
{
public UnityAction OnEventRaised;
public void RaiseEvent()
{
if (OnEventRaised != null)
OnEventRaised.Invoke();
else
{
Debug.LogWarning("Void occasion has been raised however there isn't any UnityAction related.");
}
}
}
If X has to boost an occasion E to Y and Z, then X, Y, Z should have a reference to E. For example, X should do myEvent.RaiseEvent();.
Or should you do not need to assign the occasion from the inspector, you should use Sources.Load(...); to load the occasion (as a result of it is an asset).
The Inevitable Exception
I acknowledge that occasions develop into essential when direct referencing is unattainable. For instance:
- Prefab Communication: A script on a reusable Prefab X must ship knowledge to a static, scene-based GameObject Y (which is not a prefab).
- The Drawback: Since
Prefab Xcan not maintain a tough reference toGameObject Ywithin the Inspector (asYcould not exist in each scene), occasions or world messaging develop into the one dependable method to join them, in need of utilizing costly strategies likeGameObject.FindObjectOfType().
The Query
Is my pondering right that direct entry (by way of Singletons or Inspector references) is superior for high-frequency, one-to-one communication with steady companies, and that occasions ought to primarily be reserved for situations the place:
- A number of Listeners are required (one-to-many).
- Decoupling is necessary as a result of a direct reference can’t be established (e.g., between an instantiable Prefab and a scene-only object).


