Cannot get animation to carry on between scenes

I am trying to get a little animated gameplay helper that is playing a little looped hide and seek animation behind its button to not restart its animation on scene change and I thought my script was pretty straight forward and would work. I have anything that enters the group of scenes to designate the stay active key and anything that leaves the scene to delete that key and change to the inactive key and save. I have the script plugged into the character and the button that it is hiding behind, even though the button does not need it, I just was not thinking and through the button in the script. I have the script also on an empty that is inside a prefab that the animated character is in as it is playing hide and seek behind its button. Here is my script.

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class DD_GP_MenuHelper : MonoBehaviour
{
   
    public GameObject MenuHelper;
    public GameObject MenuHelperButton;
    private void Awake()
    {
        if (PlayerPrefs.GetInt("GamePlayPrefabActive") == 1)
        {
            MenuHelper.SetActive(true);
            MenuHelperButton.SetActive(true);
           
            DontDestroyOnLoad(MenuHelper);
            DontDestroyOnLoad(MenuHelperButton);
          
           
        }
        else
        {
            if (PlayerPrefs.GetInt("GamePlayPrefabInactive") == 2)
            {
                MenuHelper.SetActive(false);
                MenuHelperButton.SetActive(false);
               
                Destroy(MenuHelper);
                Destroy(MenuHelperButton);
               
            }
        }
       
       
    }

I also tried this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PreserveMH : MonoBehaviour
{
    private static PreserveMH MenuHelper;
    void Awake()
    {
       
        DontDestroyOnLoad(transform.gameObject);

        if (MenuHelper == null)
           
           
        {
            MenuHelper = this;
        }

        else
        {
            Destroy(transform.gameObject);
        }
        bool created = false;
        if (!created)
        {
            DontDestroyOnLoad(transform.gameObject);
            created = true; // which runs "else" statement exactly the next frame )))
        }
        else
        {
            Destroy(transform.gameObject);
        }
    }
}

and this:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DontDestroy : MonoBehaviour
{
    DontDestroy instance = null;

    void Awake()
    {
        if (instance != null && instance != this)
        {
            Destroy(gameObject);
        }
        else
        {
            instance = this;
            GameObject.DontDestroyOnLoad(gameObject);
        }

        GameObject[] objs = GameObject.FindGameObjectsWithTag("MenuHelper");

        if (objs.Length > 1)
        {
            Destroy(this.gameObject);
        }
       
        DontDestroyOnLoad(this.gameObject);
       
       
       
    }

   
}

Nothing is working so far, any help is appreciated.

I would just make the button and the animation character children of the same game object, and ensure that root object is DontDestroyOnLoad. Shouldn’t take anything more than that. There shouldn’t be a need for so much ramshackle singleton code.

Or just use Additive Scene loading.

Yeah, that’s a good idea and I thought it would work, but, strangely it did not. And the additive scene loading would not work for me because the formats of the scene are the same it would conflict over what are piano keys, it is for musical education software I am making. I think it would cause problems.

I don’t see why a scene load would restart an animation. I’ve certainly never encountered it. Objects marked as DontDestroyOnLoad go into their own scenes anyway, which is how they persist across multiple scene loads.

Put it in one scene, and one scene only. Have some code that marks it as DontDestroyOnLoad and nothing else. Then initiate a scene load to see if the animation continues to play.

Your code above is the commonly defective singleton code we see around here that assume you will put it in every single scene. A singleton should never be put into more than one scene, and ideally it isn’t put into any scene at all. The best singletons can just self instantiate themselves upon being access for the first time. If they need to be referencing assets, they can pull themselves out of Resources.

So I did that, you can see the code here and it did not work. It just disappears when the next scene starts.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SimpleDD : MonoBehaviour
{
    private void Awake()
    {
        DontDestroyOnLoad(gameObject);
    }
}

Can you see the right game objects enter the DontDestroyOnLoad scene? If they disappear, that would imply something else is destroying them.

It is not in the DontDestroyOnLoad hierarchy. It listens to the destroy command. I know, I am going to keep troubleshooting. Thanks

Have you got the component on the right game object? Is said game object a root game object? Are you ignoring the warnings/errors in your console?

Well it is root under the panel of a canvas, because it has to show, I had it in another object for a bit because I need it to become inactive when a pause menu is pressed that shut off that object. But, it seems unresponsive either way to the DontDestroyOnLoad command in every way no matter how it is told to not destroy. And nothing shows up in the console over it.

If you try and DontDestroyOnLoad a non-root game object, it should log a warning telling you that this doesn’t work.

Again, needs to be on a root game object. It won’t work otherwise. So if these buttons are children of a canvas (and the canvas is a root game object), obviously put this component on said canvas.

Yeah, all the canvases are root objects. I certainly have not gotten any console errors over it.

For now I am just going to have it animated so that when it resets, it does not looks like a quick movement to evade the player in its own little way and just run an update if I can figure it out. Right now, it is a bit of a damper because it put the idea of leveled game play with a level progression based on the circle of fifths and different types of things you can do with falling crystals building up on the screen to a halt.