• Unity
  • Force Skeleton Initialization

Howdy

I have numerous scenarios where the Skeleton from a SkeletonAnimation is null:

_skeleton = _skeletonAnimation.skeleton;
if (_skeleton == null)
{
    Debug.LogError("Skeleton was null");
    return;
}

A common case where this arises is if I have a Spine object that is starting off deactivated. I will activate it and do some stuff. However if I activate it, the frame it becomes active the Skeleton is always null.

If I hide the object and have it active when the scene loads, the Skeleton is not null.

Is there a way I can force load the Skeleton the first frame it becomes active?

Related Discussions
...

The internal Skeleton is instantiated on its Awake. So don't try to access it until after it's loaded, or if it's the start of a scene, only access the skeleton on Start.
In most cases, it should be enough to add skeletonAnimation.Initialize(false); before trying to access anything on the skeletonAnimation, whether it's Skeleton or AnimationState.

It will also be null in other cases when the SkeletonData is bad (for many reasons. missing atlas. missing json. missing binary. or malformed versions of any of those).

This used to be obvious in MonoDevelop because it had better code suggestions.
But they still haven't fixed Roslyn to be able to do something similar for VS.

Oh perfecto. Calling the initialization lets me fetch a valid skeleton the same frame it wakes up.

Thank you!