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!
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).
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…
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:
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?
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");
}