question

Michael Brilz avatar image
Michael Brilz asked

Stay logged in when the scene changes

I have a GameObject with my PlayFabLogin script. Whenever I change the scene and go back to the menu, this script is empty and my data does not load. How can I fix it

I try Dontdestroy but it doesn't work

 private void OnEnable()
    {
        if (PlayFabLogin.PFL == null)
        {
            PlayFabLogin.PFL = this;
        }
        else
        {
            if (PlayFabLogin.PFL != this)
            {

                Destroy(this.gameObject);
            }
        }
        DontDestroyOnLoad(this.gameObject);
    }
Player Dataunity3ddataCharacter Data
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Hernando avatar image
Hernando answered

You should turn your PlayFabLogin script into a static class, which holds variables for you, and this class will not be attached to any GameObject, and will not be destroyed when changing scene. You can access variables under this class from anywhere.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.