'DontDestroyOnLoad not working @ scene change

I know this question have been asked before but I can still not get this to work.

I haven't really used DontDestroyOnLoad between scenes before, rather static scripts, and have problem getting this to work, the below is my latest test.

I have the following scenes:

  • SelectAndStartMatch => Script: PlayFabLogin.cs => On local GameObject ScriptContainer

i want to be able to access properties from the other scene as well as go back to SelectAndStartMatch scene with all references still there.

I have the following code in the PlayFabLogin.cs:

static PlayFabLogin instance;

private void OnEnable()
{
    if (instance == null)
    {
        instance = this;                
        DontDestroyOnLoad(gameObject);
    }
    else
    {
        if (instance != this)
        {
            Destroy(gameObject);        
        }
    }
}

I have tested a few scenarios but I still ending up with losing all references when changing scene and crash.

I would really appreciate some help to sort this out.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source