• Unity
  • [iOS] AnimationState Null References

Hey,

I am having a lot of trouble with at runtime instantiated Spine-Animation Prefabs running into a null reference with AnimationState on iOS. I have tried two solutions.

  • Deferring all Animation-Calls to Start()
    This helped with most of the cases, but there are still situations where I can run into a null reference on device.

  • Deferring all Animation calls via Coroutine
    I also tried to hack in a Coroutine, which checks the AnimationState reference per frame if it is still null, if not it starts the calls. However, I still can get a null Reference from the AnimationSate-Getter as well as sometimes having animations that never leave the coroutine (never get a non-null Animation State).

The funny thing is, the assets/prefabs seem to be ok, because some times they work, sometimes the become null at the same "place" in code as well as game-flow. We are using Skeletongraphics as well as SkeletonAnimations

Can you offer any help? When can AnimationState actually become/stay/be null? Would it be possible to have an "IsInitialized" Callback? I also noticed that the AnimationState is also null when entering OnEnable, which would be great improvement when using runtime instantiated Animations.

In Editor and on Android I do not have these kind of issues.

Related Discussions
...

AnimationState is instantiated in SkeletonAnimation.Awake.

Unless you specified Unity script execution order for that to happen very early, it will be indeterminate between builds (may or may not be why iOS does it one way and breaks and Android happened to have no problem). So it's never really safe to access and use SkeletonAnimation.state in an Awake or OnEnabled magic method.

Note that Unity's official recommendation was always to not do cross-component communication until Start.

Any time you run into this problem though, you can always call SkeletonAnimation.Initialize(false). This will ensure that AnimationState state is instantiated, even in Awake. If it stayed null, the skeleton data probably failed to load, or Unity's doing something else that's screwy.

8) Thanks for the insights,

this will help me build in a failsafe for most occurences.

Yes you are right about Start(). I just like to do my recurring initializations of pooled objects in OnEnable. Most of the code in question is just Setting up a queue of Animations for the lifetime of the object. Is there something planned to expose the AnimationState (replacing "Initial Animation") to the editor?