Adding a NamedAnimancerComponent or HybridAnimancerComponent to an object without an Animator component at runtime throws: ArgumentNullException: An Animator component is required to play animations.
Solution
Add this function to AnimancerComponent.cs:
public bool TryGetAnimator()
=> _Animator != null
|| TryGetComponent(out _Animator);
Then add if (!TryGetAnimator()) return; to the start of the following methods:
NamedAnimancerComponent.Awake
NamedAnimancerComponent.OnEnable
HybridAnimancerComponent.OnEnable
This fix will be implemented in the next version of Animancer.
Alternatives
Add the NamedAnimancerComponent or HybridAnimancerComponent to an object which already has an Animator component.
Deactivate the GameObject, add the new component, set its Animator, then reactivate it.
Environment
Description
Adding a
NamedAnimancerComponent
orHybridAnimancerComponent
to an object without anAnimator
component at runtime throws:ArgumentNullException: An Animator component is required to play animations.
Solution
Add this function to
AnimancerComponent.cs
:Then add
if (!TryGetAnimator()) return;
to the start of the following methods:NamedAnimancerComponent.Awake
NamedAnimancerComponent.OnEnable
HybridAnimancerComponent.OnEnable
This fix will be implemented in the next version of Animancer.
Alternatives
NamedAnimancerComponent
orHybridAnimancerComponent
to an object which already has anAnimator
component.GameObject
, add the new component, set itsAnimator
, then reactivate it.