Hey, I am making a game in which you have to complete some levels in the fastest time, to set a highscore. I figured out how to set the highscore, but there is one problem left.
When you are a newcomer to the game, the highscore is set to 00:00 at the beginning.
And if you look at my script…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Goal : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D coll)
{
if(coll.gameObject.name == "Player")
{
//LevelCount.levelValue += 1;
//SceneManager.LoadScene(LevelCount.levelValue);
SceneManager.LoadScene("zEndScene");
if(Timer.time < PlayerPrefs.GetFloat("Highscore"))
{
UpdateHighscore();
}
}
}
public void UpdateHighscore()
{
PlayerPrefs.SetFloat("Highscore", Timer.time);
}
}
… you see that in the if statement I am asking: If the time is lower then the highscore time, update the highscore.
But as I said, your highscore is set to 00:00 in the very beginning.
How to make it so that the first time you play, any time you get is being set as highscore value, and then, if you beat that value(Like said in the if statement), update the highscore?
I don’t use player prefs much, but it seems like Timer.time is always going to be “0” at the very beginning when you load the scene. Also, sometimes it takes awhile to load a scene, so I’m not sure how checking the time immediately after calling “LoadScene” might affect it.
If it were me, I would put in some “Debug.Log” checks in the code, maybe one right after your UpdateHighscore() function and see what the value is in the console. Then also put in a Debug.Log after you set the float in the UpdateHighscore() function as well (for the time or the highscore). That should tell you if you’re getting the values you’re expecting.
Also, do you have some sort of UI text on the screen that tells you the “highscore”. Perhaps the PlayerPrefs are updated correctly, but you’re just not updating it correctly in the UI? It’s hard to say without knowing more about the structure of your game.
Yeah well, the Debug.Logs aren’t really necessary, as I exactly know what is going on.
The highscore is always 0 at the beginning of the game, so the if statement is never going to be executed.(Because it is checking if the players time is shorter than the highscore time, but as the highscore time is set to 0 at the very first time, it can never be beaten, so it will never update).
Okay here is what I think my script should look like, I just don’t know how to do that in coding language:
SCRIPT
-If the highscore value is equal to zero and the player finishes the game, update the highscore, no matter what the time is.
Then, after that piece of script is executed(so that is when the player finishes the game for the first time), I want that piece of script to do nothing anymore for the rest of the game. From then on I want to run my if statement which is in the script I posted above, which is comparing the new score to the current highscore, and if the new time the player got is shorter than the highscore, it updates it.
So the player can play the game over and over again during one “game session” correct? If the player beats the current “high score” during the game session, you want it to update. Then if the player quits the game, and comes back again for another game session, the high score from the previous game session is still retained - and the player can play the game again and again and the high score only updates if he beats the high score?
I probably know how to do it, but I’m bad at conceptualizing the structure of people’s games lol.
I would have a “game manager” script that, in the Update function, would simply check to see if the player “finishes the game” (whatever that might be for your game), and if the player meets that condition, then check for the high score.
If you don’t mind telling me, how does the player “finish the game”?
The first things you said are true indeed. If the player comes back to the game at another point, his highscore is still there. And then when the highscore is beaten, it updates…
And my player finishes the game, when he beat all the 10 levels.(At the end of every level there is a goal, and when the player reaches the goal of the last level, my scripting will check if he beat the previous highscore).
And about that game manager, I will try something and let you know if it works…
No problem. In pseudo code, I would do something like create a new variable and call it “currentScore” or something like that (as you said before). Then when the player finishes the game you would do something like:
Okay, yeah I get what you are thinking, but the problem with this is, is that the highscore will always update when the currentscore is greater then the previous highscore(so when currentscore > highscore).
I only want that piece of code to be exexuted the first time you play.
Example:
You start the game and you see the highscore is 00:00.
Then you play the game and your time is 06:14.
Then you go back to the main menu and you see that the new highscore is 06:14.
Then you play again. Your new score is 08:11. Nothing should happen to the highscore then(and when I use your currentscore thing, it WILL update, because currentscore > highscore, and I dont want it to update at that point, from then on I only want it to update if your time is shorter then the highscore)
Then you play the game again. Your new time is 05:02. Your old highscore is beaten, so now it should update.
So to summarize the problem again:
The first time anyone plays the game and then finishes it, his time should be set as the highscore.
Then, the next times he plays the game, the highscore should only be updated if it is beaten.
Ok so there’s probably several ways to do it. But one option is to create a Boolean variable in your player prefs that will simply (and only) be for determining if it is the player’s first time playing – and then you can check that variable to see if it’s true or false whenever the player finishes the game. If you set it to true once, it should stay true forever in the player prefs correct – through every game session as long as you don’t change it? I’m assuming the player prefs is how you are getting the game to retain information over each “game session”.
So maybe create a Boolean/bool variable in the PlayerPrefs called “playerHasPlayedGameAtLeastOnce” – and initialize it to false. Then, when the player plays the game for the first time and finishes the first game, set that Boolean variable in player preferences to true.
Then everytime the player finishes a game you check it. This will have a lot to do with the “order” in which you do things in your code.
For example:
// After the first time playing the game this code will run. The second time the bool condition will be true and so it
// won’t run anytime after that. But the variable “playerHasPlayedGameAtLeastOnce” needs to be false initially in your
// game build.
if(finishGameConditionIsMet && PlayerPrefs.GetBool(“playerHasPlayedGameAtLeastOnce”) == false) {
Another thing is, if it’s impossible for the player to ever have a score of 00:00, then simply do a condition check to see if the HighScore is equal to 00:00. If it is, then set the highscore, as that would mean it is the player’s first time playing. From then on the highscore should never be 00:00… etc…
Didn’t know it was that easy. Just made an if statement which is checking if the Playerprefs.highscore == 0, and then update it if you finish the game.
Then, under that if statement, I made another if statement which is checking if your current score is shorter than the Playerprefs.highscore. If that is true, it will update.
If you get a higher value then the current highscore, it will do nothing.