Forum rules - please read before posting.

Remember Animator even when object is disabled?

Here's a strange question -- is it possible for the remember animator component remember which animation should be played even when it's parent object is disabled and then enabled again? You can watch this video and see what I mean:

https://www.loom.com/share/05efa65514754fd4a88809e642f93d3e

The remember visibility component does remember the visibility of a sprite, even when it's parent object is disabled and then reenabled. Can this be done somehow with the remember animator component?

The reason I ask, is because I've gotten some slow loading times on lower end computers and I'm trying to optimize. I figured turning off certain areas of the game would allow faster loading times. Any help would be appreciated. Thanks!

Comments

  • To clarify about Remember Visibility - this component isn't recording whether the GameObject itself is enabled. Instead, it's just looking at the enabled state of the Renderer component - which the Object: Visibility Action will affect, without affecting the enabled state of the GameObject it's attached to.

    I'm not certain Remember components are involved here, however, since you're neither saving the game nor switching scenes.

    Instead, it looks to be to do with the fact that - by default - Unity does not internally save the state of an Animator if its GameObject is disabled. You can override this, however, by setting the Animator's keepAnimatorStateOnDisable property to true, i.e.:

    using UnityEngine;

    public class KeepAnimatorState : MonoBehaviour
    {
        void Start () { GetComponent<Animator> ().keepAnimatorStateOnDisable = true; }
    }
    
  • edited April 2023

    Thanks Chris. I tried to use your proposed script, but I ended up having to modify it, since I guess Unity now uses "keepAnimatorControllerStateOnDisable":

    {
        private Animator animator;
        private void Start()
        {
            animator = GetComponent<Animator>();
            animator.keepAnimatorControllerStateOnDisable = true;
        }
    }
    

    This works while you are in a scene (enabling and disabling those parent objects). However, as you stated, this doesn't work with saved files. If you save the game and come back, the disabled animator doesn't remember the previous state when enabled.

    I'll leave the script here if anyone else has a use for it. But I think I'll have to figure something else out for optimization. Thanks!

  • Sorry, yes - to go back to your original question: a disable GameObject won't be "visible" to the save system.

    This only applies to Remember objects themselves, however - a custom Remember component on a separate, enabled, GameObject could still feasibly deal with the data of a disabled GameObect.

    You could test this by duplicating RememberAnimator, renaming the class/script name, and expose the Animator it affects as a public field.

  • edited April 2023

    Thanks again, Chris! I ended opting for different optimizations; using sprite atlases, autosaving less frequently (it was doing it every time you press a MoveDirection), and putting all non-changing animators into folders that enable/disable as you move through the game.

    I appreciate your response and it may be beneficial to someone else in the future! Thanks.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.