Hi, I'm using playerprefs in my WordSearch Game that I built with Webgl. The Issue is appear when I close my game during the game play for several times. It's automatically starts with random countdown value such as 20, 140 etc with previous score(Sometimes Not the exact score that should be saved).
Here's the code that I'm using playerpreds...
[SerializeField] public float currentLevelScore = 0.0f;
[SerializeField] private float timeLeft;
[SerializeField] private string accessToken = "";
void Awake()
{
GetPlayerPrefData();
}
void Start() {
if(timeLeft == fixedTime)
{
PauseGame();
}
//Retrive Saved Player prefs Data
if (PlayerPrefs.GetFloat("timeLeft")<150 && PlayerPrefs.GetFloat("timeLeft") > 0)
{
StartGameCanvas.SetActive(false);
}
else
{
if(PlayerPrefs.GetFloat("timeLeft") == 150)
{
StartGameCanvas.SetActive(true);
}
}
if (PlayerPrefs.GetFloat("timeLeft").Equals(0) || PlayerPrefs.GetFloat("timeLeft").Equals(null))
{
PlayerPrefs.SetFloat("timeLeft", fixedTime);
}
}
void GetPlayerPrefData()
{
accessToken = PlayerPrefs.GetString("accessToken");
currentLevelScore = PlayerPrefs.GetFloat("currentLevelScore");
float countdownTime = PlayerPrefs.GetFloat("timeLeft");
if (countdownTime <= 0)
{
timeLeft = fixedTime;
}
else
{
timeLeft = countdownTime;
}
}
//This function is calls when the Time is up or user hit the quit button
public void ResetAllData()
{
float defaultScore = 0;
string defaultAccessToken = null;
PlayerPrefs.SetFloat("currentLevelScore", defaultScore);
PlayerPrefs.SetFloat("timeLeft", fixedTime);
PlayerPrefs.SetString("timeLeft", defaultAccessToken);
PlayerPrefs.Save();
}
This issue is only occur after build the game. It's works fine in the editor.
Can anyone give me an idea or help to solve this problem? any suggestions would be highly appreciated...
↧