Hi guys can you help in my player prefs c# code of 5 highscore from the score that got in the game. Im just a noob in unity . But i need help just a simple sample code that display in gui.label the highscore
This is really easy to figure out. There are lot's of examples on the internet and video portals. Try to get started here. There you should take a look at SetInt(), GetInt() or SetFloat() and GetFloat() respectively. If you can print them (that's what the examples actually do ), you can also assign them to the text of a Label.
Its like this i try to transfer the to the value of score if its greater than the highscore But when i play it the unity it wont work. Void highscore(){ Int highscore1=0; Int highscore2=0; Int highscore3=0; score = PlayerPrefs.GetInt ("Score", 0); if (score>highscore1) { PlayerPrefs.SetInt ("Highscore1", score); PlayerPrefs.Save (); highscore1 = PlayerPrefs.GetInt ("Highscore1",0); GUI.te (new Rect (275, 100, 170, 28), "1.\t"+highscore1); } Else if(score>highscore2){ //Same as highscore1 process and also in highscore3 } }
Please use code tags. And I'm not sure whether you've written the code in the forum directly or copied it from MonoDevelop or Visual Studio, but there're lots of mistakes in it and it simply cannot work. Why do you load the current score from the player prefs? Do you save it there during the game? You locally define your highscore variables and only initialize them with 0. Hence, the score will most-likely be always higher or equal to your highscore and you'll always save the score as highscore. You have to load the highscore first, then compare it to the actual score achieved in the game and do the save logic.
Sorry that you understand the code its c# code that i use in unit MonoDevelop i'm just trying how to override the highscore if the userscore beat it Code (CSharp): //this void process when its in game over and it will place the score of the use if he/she beat the highscore(top3) Void highscore(){ GUI.Box(new Rect(40, 27,(Screen.width/4) + 200,(Screen.height/4)+ 260), "HighScoreBoard"); //value of the highscore1-3 and so the highscore can have value. Int highscore1=0; Int highscore2=0; Int highscore3=0; score = PlayerPrefs.GetInt ("Score", 0); if (score>highscore1) { //i'm trying to get the value of the score because it beat the highest PlayerPrefs.SetInt ("Highscore1", score); PlayerPrefs.Save (); //to override the highscore1 highscore1 = PlayerPrefs.GetInt ("Highscore1",0); // to display the code in the highscore board GUI.Label (new Rect (275, 100, 170, 28), "1.\t"+highscore1); } //Same process in highscore1 and also in highscore3 Else if(score>highscore2){ } }
Don't worry, I understand your code, but you still have wrong syntax in the script. First of all, it should look like: Code (CSharp): void Highscore() { GUI.Box(new Rect(40, 27, (Screen.width / 4) + 200, (Screen.height / 4) + 260), "HighScoreBoard"); int highscore1 = 0; int highscore2 = 0; int highscore3 = 0; score = PlayerPrefs.GetInt("Score", 0); if (score > highscore1) { PlayerPrefs.SetInt("Highscore1", score); PlayerPrefs.Save(); highscore1 = PlayerPrefs.GetInt("Highscore1", 0); GUI.Label(new Rect(275, 100, 170, 28), "1.\t" + highscore1); } else if (score > highscore2) { // and so on } } There are still several logical mistakes, as i've already explained in my previous post. I'm not sure whether you entirely understood what I was trying to say or not. You set your highscores to 0, then you compare them to your score, you write it back to your highscore. Next time, you simply repeat that. This cannot work as you always replace your highscore1 with the currentScore (unless it is a negative score). By the way, you can only call the method in OnGUI as you use GUI.XXXXX in it. You cannot call it somewhere else. OnGUI was and still is an expensive method, it may run several times per frame. Which also means, that you frequently load/save to the player prefs, many times per second! That's not good at all, one was never supposed to do more logic than necessary in the OnGUI method. I've written a small example on the fly, it may look more complex but it does work. You have to get your score from the script that handles your score. This example only takes the score that you can manipulate in the inspector. There's no logic that changes the score! First of all, load your highscores correctly. You should do that when your script starts (or use OnEnable instead), which handles the highscoreboard. I'm building the strings here in order to avoid doing that permantently in OnGUI. Code (CSharp): public int currentScore; private List<int> highScores = new List<int>(); private List<string> highScoresAsStrings = new List<string>(); private const int NUMBER_OF_SCORES = 3; void Start() { LoadHighScores(); } void LoadHighScores() { for (int i = 0; i < NUMBER_OF_SCORES; i++) { highScores.Add(PlayerPrefs.GetInt("highscore" + (i + 1), 0)); // we build the strings here ONCE to avoid doing it frequently in the OnGUI method highScoresAsStrings.Add(string.Format("{0}.\t{1}", i + 1, highScores[i])); } } Next, display your GUI. Call this method in OnGUI. Code (CSharp): void DrawHighScore() { GUI.Box(new Rect(40, 27, (Screen.width / 4) + 200, (Screen.height / 4) + 260), "HighScoreBoard"); int scoreYpos = 100; for (int i = 0; i < highScores.Count; i++) { GUI.Label(new Rect(275, scoreYpos, 170, 28), highScoresAsStrings[i]); scoreYpos += 30; } } Last but not least, save your score. For testing purposes (remember, this is an example, it's for you to get an idea about how to approach this) we will simulate the end of your game/round or whatever by clicking a button. Call DrawSaveButton in OnGUI. Ignore the positioning, it's only for testing. Code (CSharp): void DrawSaveButton() { if (GUI.Button(new Rect(200, 250, 170, 25), "Save")) SaveHighScores(); } // SaveHighScores is sometimes called in OnGUI, but only ONCE when you click the button // it's not called frequently and also shouldn't be called frequently void SaveHighScores() { for (int i = 0; i < highScores.Count; i++) { if (currentScore > highScores[i]) { // insert at the correct position highScores.Insert(i, currentScore); // remove the last, as it's no longer in top highscores highScores.RemoveAt(highScores.Count - 1); // save highscores and build new strings for (int j = 0; j < highScoresAsStrings.Count; j++) { PlayerPrefs.SetInt("highscore" + (j + 1), highScores[j]); highScoresAsStrings[j] = string.Format("{0}.\t{1}", j + 1, highScores[j]); } // stop the loop, as we have already added the new score to the list break; } } } I kept it pretty simple, you can do it even more structured and better. But anyway, my intention is to get you started, not to provide a perfect script for your needs.
If you are starting out I'd highly recommend using the new UI system released in 4.6.#, it's far easier to use and easier to understand. https://unity3d.com/learn/tutorials/topics/user-interface-ui
True. I always wonder why so many people still start their projects with the old GUI anyway, probably due to all the old tutorials. :/