fix #321 NRE when Event Reference Listeners event reference is not set

This commit is contained in:
Thimo de Ram 2022-03-13 16:02:15 +01:00 committed by Miika Lönnqvist
parent a35e7342cc
commit 46037ac590

View File

@ -27,9 +27,12 @@ namespace UnityAtoms
{
switch (_usage)
{
case (AtomEventReferenceUsage.VARIABLE): return _variable.GetOrCreateEvent<E>();
case (AtomEventReferenceUsage.VARIABLE_INSTANCER): return _variableInstancer.GetOrCreateEvent<E>();
case (AtomEventReferenceUsage.EVENT_INSTANCER): return _eventInstancer.Event;
case (AtomEventReferenceUsage.VARIABLE):
return _variable == null ? LogNullReference(nameof(_variable)) : _variable.GetOrCreateEvent<E>();
case (AtomEventReferenceUsage.VARIABLE_INSTANCER):
return _variableInstancer == null ? LogNullReference(nameof(_variableInstancer)) : _variableInstancer.GetOrCreateEvent<E>();
case (AtomEventReferenceUsage.EVENT_INSTANCER):
return _eventInstancer == null ? LogNullReference(nameof(_eventInstancer)) : _eventInstancer.Event;
case (AtomEventReferenceUsage.EVENT):
default:
return _event;
@ -81,5 +84,17 @@ namespace UnityAtoms
{
return reference.Event;
}
private E LogNullReference(string atomEventReferenceUsage)
{
if (AtomPreferences.IsDebugModeEnabled)
{
throw new NullReferenceException($"{atomEventReferenceUsage} reference is missing in inspector.");
}
else
{
return null;
}
}
}
}