question

maria-1 avatar image
maria-1 asked

PlayFab: Keeping Player Logged In when the Scene changes

I am having an issue with the user not staying logged in once they click the login button and the scene changes. I have a UserAccountManager.cs script that handles Awake() but I get an error in Unity that says DontDestroyOnLoad only works for rooot GameObjects or components on root GameObjects. I have the UserAccountManger script set inside my Account game object that handles the login/register panel. Here is my script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PlayFab;
using PlayFab.ClientModels;
using UnityEngine.Events;

public class UserAccountManager : MonoBehaviour
{

    public static UserAccountManager Instance;

    public static UnityEvent OnSignInSuccess = new UnityEvent();

    public static UnityEvent<string> OnSignInFailed = new UnityEvent<string>();

    public static UnityEvent<string> OnCreateAccountFailed = new UnityEvent<string>();

    public static UnityEvent<string, string> OnUserDataRetrieved = new UnityEvent<string, string>();

    string playFabID;


    void Awake()
    {
        if (Instance != null)
        {
            Destroy(gameObject);
            return;
        }else 
        {
             Instance = this;
             DontDestroyOnLoad(this.gameObject); 
        }
    }
.... and so on 
Player Data
unity-setup.png (24.9 KiB)
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

·
Rick Chen avatar image
Rick Chen answered

If you have called a PlayFab login API in a scene, the PlayFab Unity SDK will store the login session ticket and handle the authentication context. And you can directly call a PlayFab API as the logged-in player after you change scene. You don’t need to use the DontDestroyOnLoad on your game object to keep the session ticket.

“DontDestroyOnLoad only works for root GameObjects or components on root GameObjects.” This message means that the DontDestroyOnLoad can only be used on a GameObject that has no parent in the scene, the GameObject should be a root in the hierarchy. Otherwise it won’t work.

4 comments
10 |1200

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

maria-1 avatar image maria-1 commented ·

How do I call a PlayFab login API in a scene?

0 Likes 0 ·
Rick Chen avatar image Rick Chen ♦ maria-1 commented ·

For setting up PlayFab in Unity and calling APIs, you can follow this tutorial: Unity3D quickstart - PlayFab | Microsoft Learn.

0 Likes 0 ·
maria-1 avatar image maria-1 Rick Chen ♦ commented ·

Oh in that case I am already calling PlayFab Unity SDK but when I switch scenes my data isnt saved unless i do the DontDestroyOnLoad

0 Likes 0 ·
Show more comments

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.