Please help. player score saving in android

hi folks. i need help with this please:

I want to save the maximum score that the player has reached and that it can be showed when game is opened. i made this script to get the player score in game and compare it with a saved score, and if the actual score is > than the saved one then replace it. this is the first time im working with this playerprefs, i dont even know if its the right or the best methot but well… it works fine in part, it pick the runnig score the player is acheiving during game, but if i open two levels its like if values go chained and shows something like 12345667… its a mistake i made in the script? my future idea is that player can save his name along with the score… its for android, just in case…

Static var POINTS=0; //(in other script, picks scores during game)

var Mpoints=0; // take score in POINTS and shows it in gui

var storedpoints=0; //picks saved score in playerpref to compare with Mpoints
function Start(){ 

Time.timeScale = 1.0; 

Mpoints= pointscontrol.POINTS;

storedpoints=(PlayerPrefs.GetInt("savedscore"));

if (storedpoints>Mpoints){

Mpoints = storedpoints;

}

}

script continues with other stuff…

function OnGUI() {
//MAX POINTS

    GUI.Box (Rect (Screen.width-90,Screen.height-40,80,40), "Max points");

	GUI.skin = menSkin;

	GUI.Label(Rect(Screen.width-80,Screen.height-25,70,40),Mpoints.ToString());

	if (Mpoints>storedpoints){

	PlayerPrefs.SetFloat("savedscore", Mpoints);

}}

from now thanks for ur help.

From your explanation of the results it seems currently your untyped variables are cast to strings , try declaring data types for your variables.

Can you be more specific or show example. I have this question too.

From a quick glance, I see one problem: savedscore is set as a float, but you get an int (use int or float consistently, and probably int would be best).

yes ncst good eye , of coarse datatyping your variables should help catch errors of this nature at compile time ( although Im not sure for unity script classes if pragma is not strict ) .

So if your score does not have floating point values use GetInt/SetInt, and get into the practice of declaring your variables datatypes

eg:

Static var POINTS:int = 0;

var Mpoints:int = 0;

var storedpoints:int = 0;

kaiser im going to pm you about script. I need that kind of script for my game. :slight_smile:

ok irish go ahead.

and thanks shader i will try doing it that way and see if it works… but overall u think the script is ok? as its my first time working with these playerprefs i dont know if its the best way to save variables in the phone to be gathered later.

Well I cant really dive too deep into a whole discussion on good code vs bad code , keep learning and looking at code , learn about the specific language rules , design pattern structures etc… there is always room to improve. As you progress you will start to become better at writing “good” code… well if all that interests you…

For example … you should not have this kind of code in your GUI function

 if (Mpoints>storedpoints){

 

    PlayerPrefs.SetFloat("savedscore", Mpoints);

OnGUI can be called multiple times per frame , dont use it as a means to perform validation steps on these variables . Rather use Update or LateUpdate which is only called once per frame.

lol you refuse to learn.

Why try making a mmo if you cant do very basic scripting. I say go do some tutorials.
Just keep riding with others work.

Well changing the vars to int didnt changed anything. still having the same problem: play, score = 100, ok… play again score 100 + 100 = 200, fine. but when i go to the main menu, where is supossed to be shown the max score appears something like 102392200…

did you also change all your calls to playerprefs involving your int variables to use GetInt or SetInt ? just wondering as it was noted by ncst you had to two different methods in your code … outside of that it hard to tell as these are only snippits of code here from a whole project , it might be a logic error somewhere , something you are overlooking. Put a few Debug.Log statements in at all your pre write post write nodes of logic and narrow down where exactly the error appears

I already changed playerprefs calls to int. Now im not getting any error or crash but still not saving my score… Is there a way to save this score to a txt file to be readen later?