SceneLinkedSMB on disable loosing references to monobehaviour?

Unity 2018.4.3f1

I am using the SceneLinkedSMB class from the 3D kit and having an unexpected behavior. Some of our objects containing animation behaviors get disable on start and then enabled later in the game.

On the call to Awake I initialize the animation links something like this:

 protected void Awake()
        {
        animator = GetComponent<Animator>();
         SceneLinkedSMB<AnimationManager>.Initialise(animator, this);
   
            }
        }

This gets called at the start of the game. Then when they get activated the Start gets called with this:

Log.InfoFormat("Start AnimationManager on " + gameObject.name + " " + animator.GetInstanceID(), gameObject);
            AnimationBehaviour[] sceneLinkedSMBs = animator.GetBehaviours<AnimationBehaviour>();
            foreach (AnimationBehaviour behaviour in sceneLinkedSMBs)
                {
                Log.InfoFormat("Found behaviour " + behaviour.name + " with mono " + behaviour.m_MonoBehaviour, gameObject);
                }

On the Start the m_MonoBehaviour is null even though it was set on Awake call to SceneLinkedSMB.Initialise(animator, this);

But if the object is not deactivated on start this works fine and m_MonoBehaviour is/remains assigned in Start as it should.

Is there something going on with SceneLinkedSMB during the disabling of objects? This behaviour seems strange…

Try setting keepAnimatorControllerStateOnDisable. The Animator deallocates some things when disabled so I’m assuming that’s what’s happening to the SceneLinkedSMB, then a new instance gets created when it’s reactivated.

Or you could always just initialise it in OnEnable instead of Awake.

2 Likes

Ohhh, Thank you! I will give that a try. I came up with a solution. But this sounds more like exactly what I need!

Thanks.

1 Like