FAQ

Event Listener

Do I need to use an EventListener to receive messages from ScriptableEvents?

Not necessarily. EventListeners are useful if you want to trigger logic from the inspector using UnityActions. However, if you don’t want to invoke a response through the inspector but rather by code, you can register to the OnRaised method of ScriptableEvents from code. Simply expose a reference to the ScriptableEvent and register / unregister from code. Here is a simple example:

using Obvious.Soap;
using UnityEngine;

public class EnableGameObjectOnEvent : MonoBehaviour
{
    [SerializeField] private ScriptableEventNoParam _scriptableEvent;

    private void Awake()
    {
        _scriptableEvent.OnRaised += OnEventRaised;
    }
    
    private void OnDestroy()
    {
        _scriptableEvent.OnRaised -= OnEventRaised;
    }

    private void OnEventRaised()
    {
        gameObject.SetActive(true);
    }
}

Last updated