Skip to content

Commit 21cab92

Browse files
author
Jo Simard
committed
Update Injector.cs
Attempt to create a descriptive error message for quick resolution of UnityEngine.Component injection problems.
1 parent b828357 commit 21cab92

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/Assets/Adic/Scripts/Framework/Injection/Injector.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,9 @@ protected void InjectFields(object instance, SetterInfo[] fields) {
327327
var field = fields[fieldIndex];
328328
var valueToSet = this.Resolve(field.type, InjectionMember.Field, instance, field.identifier);
329329
field.setter(instance, valueToSet);
330+
#if UNITY_EDITOR
331+
ValidateInjection(instance, valueToSet, field.type);
332+
#endif
330333
}
331334
}
332335

@@ -340,6 +343,27 @@ protected void InjectProperties(object instance, SetterInfo[] properties) {
340343
var property = properties[propertyIndex];
341344
var valueToSet = this.Resolve(property.type, InjectionMember.Property, instance, property.identifier);
342345
property.setter(instance, valueToSet);
346+
#if UNITY_EDITOR
347+
ValidateInjection(instance, valueToSet, property.type);
348+
#endif
349+
}
350+
}
351+
352+
/// <summary>
353+
/// Verify if injection is problematic and inform user accordingly
354+
/// </summary>
355+
protected void ValidateInjection(object instance, object value, Type valueType)
356+
{
357+
// Instance resolved to 'null' when Components are instanciated, an error will be logged soon after.
358+
if (instance.ToString() == "null") return;
359+
360+
if(value is UnityEngine.Component)
361+
{
362+
UnityEngine.Component component = value as UnityEngine.Component;
363+
if(component == null || component.gameObject==null)
364+
{
365+
UnityEngine.Debug.LogError("Could not inject '" + valueType.Name + "' from '" + instance.GetType() + "'. Please review your bindings values and order.");
366+
}
343367
}
344368
}
345369

@@ -538,4 +562,4 @@ protected void OnBeforeAddBinding(IBinder source, ref BindingInfo binding) {
538562
}
539563
}
540564
}
541-
}
565+
}

0 commit comments

Comments
 (0)