0
\$\begingroup\$

I'm working on a Unity project where I have a Singleton class, XPmanager, which is responsible for managing XP data across multiple scenes. I'm using the DontDestroyOnLoad method to make sure the XPmanager instance is preserved when switching between scenes.

However, I've noticed that when I switch to another scene and then return back to the main scene, the Awake() method of my XPmanager is called twice, and the Instance IDs are different, indicating that a new instance is being created. This results in the loss of the XP data.

public class XPmanager : MonoBehaviour
{
    public static XPmanager singleton;

    // Other fields ...

    private void Awake()
    {
        if (singleton == null)
        {
            singleton = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }

        // ...
    }

    // Other methods ...
}

I've checked that there's only one instance of the XPmanager in my project. Despite using DontDestroyOnLoad, the Awake() method is called again with a different Instance ID when I switch back to the main scene.

Why is this happening, and how can I ensure that only a single instance of the XPmanager is preserved across scene changes?

\$\endgroup\$
5
  • \$\begingroup\$ How are your domain reloading settings configured? Skipping domain reload speeds up entering/exiting play mode or updating after code changes in the editor, but it can introduce unexpected bugs into scripts that rely on static variables. \$\endgroup\$
    – DMGregory
    Apr 30, 2023 at 16:47
  • \$\begingroup\$ i enable the "Reload Domain" and "Reload Scene" , but still awake twice. and i use SceneManager.LoadScene("MainScene"); to return the mainscene \$\endgroup\$ Apr 30, 2023 at 17:04
  • 1
    \$\begingroup\$ I notice you don't have a return after Destroy(gameObject). Is there code in that // ... section that's being run by the instance that's in the process of self-destructing, because it didn't early-out because of that? If not, can you show a more complete example that clarifies exactly what symptoms show up, and what lines of code relate to those symptoms? \$\endgroup\$
    – DMGregory
    Apr 30, 2023 at 17:29
  • \$\begingroup\$ i add return but seen no work for me.......and i use SceneManager.LoadScene("MainScene") to return to the main scene. Is it possible that calling LoadScene causes Unity to reload my existing singleton manager? \$\endgroup\$ Apr 30, 2023 at 19:14
  • \$\begingroup\$ gamedev.stackexchange.com/questions/116009/… \$\endgroup\$
    – Zibelas
    Apr 30, 2023 at 19:28

1 Answer 1

2
\$\begingroup\$

DontDestroyOnLoad only works for root GameObjects or components on root GameObjects.

I set all my singleton managers as children of an emptygameobject and doing so caused my problem.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Don't forget to pay close attention to your developer console window - Unity generates error messages when you make this mistake that alert you to the problem. These error messages should always be included in your question if you need help on how to interpret and resolve them. \$\endgroup\$
    – DMGregory
    Apr 30, 2023 at 20:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .