• RuntimesUnity
  • How to play an SkeletonGraphic animation in Unity?

I'm using the skeletonGraphic component.
Trying to play the animation, an error occurs "NullReferenceException: Object reference not set to an instance of an object".

The animation fields contain animation references. There is a debug in the code - a control for an empty value.
Why does the error occur?

using Spine.Unity;

public class PlayerTopMenuController : MonoBehaviour
{
    [SerializeField] private AnimationReferenceAsset addLiveAnimation, lostLiveAnimation;
    private SkeletonGraphic skeletonGraphic;


    void Start()
    {
        skeletonGraphic = GetComponent<SkeletonGraphic>();
        PlayAnimations();
    }

    private void OnEnable()
    {
        GameActions.onPlayerLivesChanged += PlayAnimations;    
    }


    private void Disable()
        GameActions.onPlayerLivesChanged -= PlayAnimations;
    }


    private void PlayAnimations()
    {
     SetAnimation(addLiveAnimation, true, 1f);
    }

    public void SetAnimation(AnimationReferenceAsset animation, bool loop, float timeScale)
    {
        Debug.Log(animation.name);
        if (animation==null) Debug.Log("Animation is null");

        skeletonGraphic.AnimationState.SetAnimation(0, animation, loop); // NullReferenceException: Object reference not set to an instance of an object
    }
}

PS
I tried to use the code from the theme
https://ru.esotericsoftware.com/forum/d/8090-skeletongraphic-animations-in-unity

and it didn't help.

GPT chat also could not find a solution.

    Related Discussions
    ...

    The error "NullReferenceException: Object reference not set to an instance of an object" typically occurs when you try to access a member on a null object. In your case, it seems that skeletonGraphic.AnimationState is null. This can happen if the SkeletonGraphic component is not properly initialized.

    To resolve this issue, ensure that the SkeletonGraphic component is properly initialized before you attempt to set an animation. You can do this by calling the Initialize method on the SkeletonGraphic component. Here is an updated version of your script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using Spine.Unity;
    
    public class PlayerTopMenuController : MonoBehaviour
    {
        [SerializeField] private AnimationReferenceAsset addLiveAnimation, lostLiveAnimation;
        private SkeletonGraphic skeletonGraphic;
    
        void Start()
        {
            skeletonGraphic = GetComponent<SkeletonGraphic>();
            skeletonGraphic.Initialize(false); // Ensure the SkeletonGraphic is initialized
    
            UpdateLivesCount();
        }
    
        private void OnEnable()
        {
            GameActions.onPlayerLivesChanged += UpdateLivesCount;
        }
    
        private void OnDisable()
        {
            GameActions.onPlayerLivesChanged -= UpdateLivesCount;
        }
    
        public void UpdateLivesCount()
        {
            PlayAnimations();
        }
    
        // Play animations
        private void PlayAnimations()
        {
            SetAnimation(addLiveAnimation, true, 1f);
        }
    
        public void SetAnimation(AnimationReferenceAsset animation, bool loop, float timeScale)
        {
            Debug.Log(animation.name);
            if (animation == null) Debug.Log("Animation is null");
    
            skeletonGraphic.AnimationState.SetAnimation(0, animation, loop); // NullReferenceException: Object reference not set to an instance of an object
        }
    }

    Make sure that the SkeletonGraphic component is properly initialized by calling skeletonGraphic.Initialize(false); in the Start method. This should resolve the NullReferenceException error.

    Additionally, ensure that your AnimationReferenceAsset fields (addLiveAnimation and lostLiveAnimation) are properly assigned in the Unity Inspector.

    EIvanov Trying to play the animation, an error occurs "NullReferenceException: Object reference not set to an instance of an object".

    Where does the NullReferenceException occur? Please always provide the full error message.

    The referenced thread is from 2017, any information from 7 years ago has a high probability of being obsolete or even wrong. Please have a look at the example projects that come with the spine-unity unitypackage.

    EIvanov There is a debug in the code - a control for an empty value.

    Debug.Log(animation.name);
    if (animation==null) Debug.Log("Animation is null");```

    This second line will never be reached if animation is null, as the first line will throw an exception already.

    EIvanov Why does the error occur?

    If you tell us where exactly the exception occurs, we can help.
    Also check if your addLiveAnimation property has an asset assigned. If so, please select this asset and check whether the asset's parameters look ok in the Inspector or whether there is anything wrong perhaps. You can also regenerate the animation reference assets via the Create Animation Reference Assets in the SkeletonDataAsset Inspector (see here).

      Harald I simplified the code as much as possible to understand where the error is.

      using Spine.Unity;
      
      public class PlayerTopMenuController : MonoBehaviour
      {
      
          [SerializeField] private AnimationReferenceAsset addLiveAnimation, lostLiveAnimation;
          private GameObject liveAnimation;
          private SkeletonGraphic liveAnimationSkeletonGraphic;
      
          void Start()
          {      
              liveAnimation = transform.Find("LiveAnimation").gameObject;
              liveAnimationSkeletonGraphic = liveAnimation.GetComponent<SkeletonGraphic>();
              if (liveAnimationSkeletonGraphic == null) Debug.Log("liveAnimationSkeletonGraphic is null");
      	if (addLiveAnimation == null) Debug.Log("addLiveAnimation is null"); // In console i see addLiveAnimation is null, why it null?
      		
              SetAnimation(addLiveAnimation, true, 1f); // 16 NullReferenceException here
          }
      
      
          public void SetAnimation(AnimationReferenceAsset animation, bool loop, float timeScale)
          {
       
              if (animation==null) Debug.Log("Animation is null"); // In console i see Animation is null, why it null?
      		
              Debug.Log(animation.name); // 24 NullReferenceException here
      		
              liveAnimationSkeletonGraphic.AnimationState.SetAnimation(0, animation, loop);
          }
      }

      Animation not found. But it is in the field. addLiveAnimation in the serializable field.
      Further errors are consequences of empty animation...

      NullReferenceException: Object reference not set to an instance of an object
      PlayerTopMenuController.SetAnimation (Spine.Unity.AnimationReferenceAsset animation, System.Boolean loop, System.Single timeScale) (at Assets/GameObjects/Player/PlayerInterface/Scripts/PlayerTopMenuController.cs:24)
      PlayerTopMenuController.Start () (at Assets/GameObjects/Player/PlayerInterface/Scripts/PlayerTopMenuController.cs:16)

      Image

      @Elvanov Thanks for the additional info. When an asset reference is assigned in the Inspector and is null at runtime, that's an issue which is likely independent of Spine. You might want to also search the Unity forums on what may cause such an issue.

      Perhaps there is a problem with the PlayerTopMenu prefab or something like that. Does the issue also occur if you create a normal scene with the GameObjects from the prefab added directly and without using a prefab?

      Do you use any form of delayed loading mechanisms, like addressables, asset bundles or the like?