Why does my Playerprefs (Unity) not work on Android?

Asked

Viewed 392 times

0

That’s my first question and first post here!

Well, I’m playing a game on Unity android-facing and created a javascript to save and calculate some values.

Here’s how it works: My game has some sliders that I use as my character’s status. These sliders lose points every second, as a hunger system. It turns out that if I close the game, the value reset. Then I discovered the Playerprefs. I made a javascript that, when closing the game, he caught the system date (seconds, minutes ... months and year) and guarded. When he opened up, he carried that data, transformed everything in seconds, took the current date of the system and gave the difference (also in seconds) of how long the game was closed. Based on this, it removes the points from the bar that would be lost if the game was open.

All very well, working in the cute Unity. Closes, loads, closes Unity, open, and it still works fine. I put on the phone... Nothing. I uninstalled, installed, cleaned data, and nothing working.

What am I doing wrong? Do I have to set something up in Unity? My code is wrong?

Below is the javascript:

#pragma strict

//----------------------- Variaveis dos resultados -----------------------

//----------------------- Variaveis dos resultados -----------------------



//----------------------- Variaveis das Barras -----------------------

public var BarraFome : UnityEngine.UI.Slider;

public var BarraFelicidade : UnityEngine.UI.Slider;

public var BarraBanho : UnityEngine.UI.Slider;

public var BarraDormir : UnityEngine.UI.Slider;

//----------------------- Variaveis das Barras -----------------------






function Start(){


//-Pega o tempo atual

var dtnow : System.DateTime = System.DateTime.Now;

//-Pega o tempo atual


//--------------------------- Calculo das diferenças dos tempos ---------------------------


//- Pega todos os tempos da ultima vez que abriu

var dtoldSec = PlayerPrefs.GetInt("oldTimeSecond");
var dtoldMin = PlayerPrefs.GetInt("oldTimeMinute");
var dtoldHour = PlayerPrefs.GetInt("oldTimeHour");

var dtoldDay = PlayerPrefs.GetInt("oldTimeDay");
var dtoldMonth = PlayerPrefs.GetInt("oldTimeMonth");
var dtoldYear = PlayerPrefs.GetInt("oldTimeYear");

//- Pega todos os tempos da ultima vez que abriu



//- Calcula a diferença entre os segundos

var resultadoSeg = dtnow.Second - dtoldSec;


//- Calcula a diferença entre os segundos



//- Calcula a diferença entre os minutos e transforma para segundos

var resultadoMin = (dtnow.Minute - dtoldMin) * 60;

//- Calcula a diferença entre os minutos e transforma para segundos



//- Calcula a diferença entre as horas e transforma para segundos

var resultadoHora = (dtnow.Hour - dtoldHour) * 3600;

//- Calcula a diferença entre as horas e transforma para segundos



//- Calcula a diferença entre os dias e transforma para segundos

var resultadoDias = (dtnow.Day - dtoldDay)* 86400;

//- Calcula a diferença entre os dias e transforma para segundos



//- Calcula a diferença entre os meses e transforma para segundos

var resultadoMeses = (dtnow.Month - dtoldMonth)* 2592000;

//- Calcula a diferença entre os meses e transforma para segundos



//- Calcula a diferença entre os anos e transforma para segundos

var resultadoAno = (dtnow.Year - dtoldYear)* 31536000;

//- Calcula a diferença entre os anos e transforma para segundos






//- Calcula o resultado total

var resultadoTotal = resultadoSeg + resultadoMin + resultadoHora + resultadoDias + resultadoMeses + resultadoAno;


print ("resutado: " + resultadoTotal);

//- Calcula o resultado total



//--------------------------- Calculo das diferenças dos tempos --------------------------- 




//--------------------------- Calculo das Barras ---------------------------


var FomeBarra = PlayerPrefs.GetFloat("FomeValor");

var FelicidadeBarra = PlayerPrefs.GetFloat("FelicidadeValor");



BarraFome.value = FomeBarra - resultadoTotal;

BarraFelicidade.value = FelicidadeBarra - resultadoTotal;


PlayerPrefs.Save();
//--------------------------- Calculo das Barras ---------------------------



}

function Update()
{


}

function CurrentTime() { 

var dt : System.DateTime = System.DateTime.Now;

var d : int = dt.Day;

var mo : int = dt.Month;

var y : int = dt.Year;

var h : int = dt.Hour; 

var m : int = dt.Minute; 

var s : int = dt.Second;


}


function OnApplicationQuit() {



    var dt : System.DateTime = System.DateTime.Now;




//-------------- Guarda o Valor das Barras --------------


    PlayerPrefs.SetFloat("FomeValor", BarraFome.value);

    PlayerPrefs.SetFloat("FelicidadeValor", BarraFelicidade.value);


//-------------- Guarda o Valor das Barras --------------           



//-------------- Guarda o Valor dos Tempos --------------   

    PlayerPrefs.SetInt("oldTimeSecond", dt.Second);

    PlayerPrefs.SetInt("oldTimeMinute", dt.Minute);

    PlayerPrefs.SetInt("oldTimeHour", dt.Hour);


    PlayerPrefs.SetInt("oldTimeDay", dt.Day);

    PlayerPrefs.SetInt("oldTimeMonth", dt.Month);

    PlayerPrefs.SetInt("oldTimeYear", dt.Year);


    PlayerPrefs.Save();


//-------------- Guarda o Valor dos Tempos --------------


}
  • Did you check how Playerpref is on the device, to see if you actually wrote anything? - you will need root for the data directory. Another way is to check if something is coming back with Playerprefs.Haskey (key). I found it strange to use . Save() all the time. This is for a "checkpoint" or something, Unity makes it automatic for you when the game closes normally. By the way, playerprefs is to use in small things, player preferences, if you plan to use it as savegame will start to find problems as its size increases.

  • Another way is to use the signature of the method that returns a default value if nothing is found.

  • Thanks! But I decided to switch from Onapplicationquit(), to Onapplicationpaused(). The game is paused before closing, so I think it makes sense...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.