Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question DontDestroyOnLoad() duplicates when switch scenes

Discussion in 'Scripting' started by zeinhatduong, Apr 9, 2023.

  1. zeinhatduong

    zeinhatduong

    Joined:
    Sep 18, 2021
    Posts:
    13
    I'm making a 2D platformer game and when I switch scenes to another world, I'm using DontDestroyOnLoad() to keep all the data intact. But that means whenever I switch back scenes again, in the DontDestroyOnLoad() list duplicates the objects. (by the way, I put the DontDestroyOnLoad() in the Start() function, that might be the reason why.)
     
  2. zeinhatduong

    zeinhatduong

    Joined:
    Sep 18, 2021
    Posts:
    13
    And also, when I switch scenes, is there any way that it enters in a certain position?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    I have already told you the step to take here;

    https://forum.unity.com/threads/res...ealth-in-between-scenes.1422564/#post-8935527

    Specifically YOU MUST:

    - identify parts of your game that live longer than a scene <-- essential first step; without this you are lost

    - identify how long they live (game manager: life of the game ... music / menu manager: forever? etc)

    - implement the parts so that they have the appropriate lifecycle

    But you did NOT take that step because you posted here:

    https://forum.unity.com/threads/res...ealth-in-between-scenes.1422564/#post-8936790

    and you said:

    Simply saying "i just used DontDestroyOnLoad()" is NOT a shortcut for the three steps I listed above.
     
  4. zeinhatduong

    zeinhatduong

    Joined:
    Sep 18, 2021
    Posts:
    13
    Okay then, here's my list

    • Player
      • The whole game
    • (I don't know if relevant or not) Health
      • The whole game (Goes with the player)
    • Game Manager
      • The whole game
    • Camera
      • The whole game because I want it to follow and keep track of the player, or else the new camera will have to find the player and that might not work
    • Enemies
      • They only follow you for one scene or level
    • Boost enemies
      • They follow you around in the boost zone to give you a health boost (only one scene)

    What do I do now with this list?
     
  5. zeinhatduong

    zeinhatduong

    Joined:
    Sep 18, 2021
    Posts:
    13
    Is the Awake() method the same as Start()?
     
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,184
    To avoid duplicate copies of a script you need to check if an instance of it already exists, and if it does already exist destroy the new copy of it.

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class MyPersistentObject : MonoBehaviour
    4. {
    5.     public static MyPersistentObject Instance;
    6.  
    7.     private void Awake()
    8.     {
    9.         if (Instance == null)
    10.         {
    11.             Instance = this;
    12.             DontDestroyOnLoad(gameObject);
    13.         }
    14.         else if (Instance != this)
    15.         {
    16.             Destroy(gameObject);
    17.         }
    18.     }
    19. }
    No.

    Guide-to-the-Execution-Order-of-Unity-Event-Functions-V2-Light.png
     
    Last edited: Apr 10, 2023
  7. zeinhatduong

    zeinhatduong

    Joined:
    Sep 18, 2021
    Posts:
    13
    Using your code

    Code (CSharp):
    1. using UnityEngine;
    2. public class MyPersistentObject : MonoBehaviour
    3. {
    4.     public static MyPersistentObject Instance;
    5.     private void Awake()
    6.     {
    7.         if (Instance == null)
    8.         {
    9.             Instance = this;
    10.             DontDestroyOnLoad(gameObject);
    11.         }
    12.         else if (Instance != this)
    13.         {
    14.             Destroy(gameObject);
    15.         }
    16.     }
    17. }
    apparently seems to temporarily work. However, I still see some duplicates in the scene, except, they're just like dead objects.

    https://ibb.co/S5t4nfM ⬅️ image link

    In this image, you can see that the camera is focused on the DontDestroyOnLoad() object, but there is still a duplicate of every object, just not used somehow.