Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Cannot get animation to carry on between scenes

Discussion in 'Scripting' started by justinmed, May 9, 2024.

  1. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    79
    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.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using Unity.VisualScripting;
    5. using UnityEngine;
    6.  
    7. public class DD_GP_MenuHelper : MonoBehaviour
    8. {
    9.    
    10.     public GameObject MenuHelper;
    11.     public GameObject MenuHelperButton;
    12.     private void Awake()
    13.     {
    14.         if (PlayerPrefs.GetInt("GamePlayPrefabActive") == 1)
    15.         {
    16.             MenuHelper.SetActive(true);
    17.             MenuHelperButton.SetActive(true);
    18.            
    19.             DontDestroyOnLoad(MenuHelper);
    20.             DontDestroyOnLoad(MenuHelperButton);
    21.          
    22.            
    23.         }
    24.         else
    25.         {
    26.             if (PlayerPrefs.GetInt("GamePlayPrefabInactive") == 2)
    27.             {
    28.                 MenuHelper.SetActive(false);
    29.                 MenuHelperButton.SetActive(false);
    30.                
    31.                 Destroy(MenuHelper);
    32.                 Destroy(MenuHelperButton);
    33.                
    34.             }
    35.         }
    36.        
    37.        
    38.     }

    I also tried this:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PreserveMH : MonoBehaviour
    6. {
    7.     private static PreserveMH MenuHelper;
    8.     void Awake()
    9.     {
    10.        
    11.         DontDestroyOnLoad(transform.gameObject);
    12.  
    13.         if (MenuHelper == null)
    14.            
    15.            
    16.         {
    17.             MenuHelper = this;
    18.         }
    19.  
    20.         else
    21.         {
    22.             Destroy(transform.gameObject);
    23.         }
    24.         bool created = false;
    25.         if (!created)
    26.         {
    27.             DontDestroyOnLoad(transform.gameObject);
    28.             created = true; // which runs "else" statement exactly the next frame )))
    29.         }
    30.         else
    31.         {
    32.             Destroy(transform.gameObject);
    33.         }
    34.     }
    35. }
    36.  
    37.  
    38.  
    39.    
    40.  


    and this:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class DontDestroy : MonoBehaviour
    7. {
    8.     DontDestroy instance = null;
    9.  
    10.     void Awake()
    11.     {
    12.         if (instance != null && instance != this)
    13.         {
    14.             Destroy(gameObject);
    15.         }
    16.         else
    17.         {
    18.             instance = this;
    19.             GameObject.DontDestroyOnLoad(gameObject);
    20.         }
    21.  
    22.         GameObject[] objs = GameObject.FindGameObjectsWithTag("MenuHelper");
    23.  
    24.         if (objs.Length > 1)
    25.         {
    26.             Destroy(this.gameObject);
    27.         }
    28.        
    29.         DontDestroyOnLoad(this.gameObject);
    30.        
    31.        
    32.        
    33.     }
    34.  
    35.    
    36. }
    37.  
    38.  
    Nothing is working so far, any help is appreciated.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,161
    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.
     
  3. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    79
    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.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,161
    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.
     
  5. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    79
    So I did that, you can see the code here and it did not work. It just disappears when the next scene starts.
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class SimpleDD : MonoBehaviour
    7. {
    8.     private void Awake()
    9.     {
    10.         DontDestroyOnLoad(gameObject);
    11.     }
    12. }
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,161
    Can you see the right game objects enter the DontDestroyOnLoad scene? If they disappear, that would imply something else is destroying them.
     
  7. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    79
    It is not in the DontDestroyOnLoad hierarchy. It listens to the destroy command. I know, I am going to keep troubleshooting. Thanks
     
  8. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,161
    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?
     
  9. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    79
    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.
     
  10. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,161
    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.
     
  11. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    79
    Yeah, all the canvases are root objects. I certainly have not gotten any console errors over it.
     
  12. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    79
    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.