Issue with Retrieving PlayerPrefs Value Across Scripts in Unity

Hi everyone,

I’m encountering an issue with PlayerPrefs in Unity. Specifically, I am retrieving the URL using Javascript and then in a script I am extracting a part of this URL and saving it in the PlayerPrefs.

Problem is when I try to retrieve it in a different script a scene later the PlayerPrefs value is not there. I have worked with PlayerPrefs a couple of times and it always worked. Can’t explain it this time…

using UnityEngine;

public class LimesurveyID : MonoBehaviour
{

    // This method will be called by JavaScript with the URL as a parameter
    public void ReceiveURLFromBrowser(string gameURL)
    {
        Debug.Log("Received URL: " + gameURL);

        string[] urlParts = gameURL.Split('=');
        if (urlParts.Length == 2)
        {
            string limeID = urlParts[1];
            PlayerPrefs.SetString("LimeID", limeID);
            PlayerPrefs.Save();
            Debug.Log("LimeID: " + PlayerPrefs.GetString("LimeID"));

        }
        else
        {
            Debug.LogWarning("URL does not contain a valid LimeSurvey ID.");
        }
    }
}

The value gets saved and is shown in the debug.log.

Here is how I tried to retrieve it in the next scene for example:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    void Start()
    {
        string limeID = PlayerPrefs.GetString("LimeID", "No LimeID found");
        Debug.Log("Retrieved LimeID: " + limeID);
    }
}

Any help would be greatly appreciated - thank you in advance!

Many questions:

  1. So, does it work the next time you run the game
  2. when is the object test script is on get made in relation to LimesurveyID
  3. do you at any time delete the saved value, or file, or have any other save limeID code
  1. It never works no, always says “no LimeID found”

  2. The Limesurvey ID Object is attached to the Main Menu of my Game (Scene 1), the Test script is attached in the following Scene which is the Tutorial Level (Scene 2).

  3. No, it is saved and then retrieved in the next scene, no other scripts etc. interfering

It’s very weird, I have been working with PlayerPrefs a lot in this project but never had something like this happen. I was thinking it had something to do with the javascript but why should it? The URL which is used for the PlayerPrefs is already there so to say…

You should avoid spattering this sort of messy code all over the place:

Instead use this to keep things tidy and avoid silly typing mistakes in the string:

Here’s an example of simple persistent loading/saving values using PlayerPrefs:

Useful for a relatively small number of simple values.

When you use this structure you can quickly transiently replace it with a simple:

public static string LimeID;

… and prove that your logic flows in the correct order / sequence.

It also lets you trivially breakpoint 100% of the accesses to that property and catch unwanted out-of-sequence accesses.

I’m guessing you just have a bug.

Thanks for your input!
The issue unfortunately still persists…

For anyone wondering and/ or having a similar problem. Seems to be a problem with the javascript.
I got it to work using @Bunny83 Solution from this post:

How to read URL parameters from Unity WebGL Build - #4 by Bunny83

Cheers!

Hi @jlnxaer
The PlayerPrefs should work normally on the Web platform and also persist when reloading the page.
What browser are you using and are there any errors in the browsers console?

Hi!
There were no errors at all. I testet it in Safari and Chrome

I recall there is an issue about the persistence of using PlayerPrefs (you may search on youtube) that I am hesitated to using it. I am not sure but in this quick test, the “Test4” is never printed out on unity console.

public void Testing(){
        Debug.Log("Test1drrg");
        // string tempUID = PlayerPrefs.GetString("UID", null);
        Debug.Log("Test2: ");;
        string loginType = PlayerPrefs.GetString("LoginType", "NO LoginType");
        Debug.Log("Test4");
    }