I’m making this game that has a timer counting up the time from the start of the level, which resets every time you die. I want to (1) save the score, (2) check to see if it is higher than the previously stored score, (3) if the new score is higher than the previous score, replace the previous score with the new score and (4) display the new score as GUI text. I already have the GUI text set up, all I need to do is give it the score to display, which is what I’m hoping to get some help with here. I’ve already set up the timer that counts up and resets when you die. I’m new to UnityScript, so you may have to dumb things down for me and I apologize for that. This is one of the last things I need for my game. I want answers in UnityScript, please.
You could use PlayerPrefs to save the score between sessions; have a look at:
Specifically for saving your score, you’ll probably want to use:
In your case, it might look something like:
var score : int = 0;
if (score > PlayerPrefs.GetInt("Score")) {
PlayerPrefs.SetInt("Score", score);
}
Thanks for the answer, but I need more clarity. Take a look at the attachment I’ve added to see what I’m trying to do in an image. Please look at the image attachment before reading any further! Here’s exactly what I’m trying to do in text:
- When Fish collides with any Hook, check the Current Time.
- If Current Time is greater than the Best Time, replace Best Time with the Current Time.
- Save the Best Time forever and restart the scene.
Here is the collision function for the Fish:
function OnCollisionEnter2D (col : Collision2D)
{
if (col.gameObject.CompareTag ("Obstacle"))
{
Application.LoadLevel ("gameScene");
}
}
NOTE: In the above code, the tag “Obstacle” refers to the Hooks.
Here is the code for the Current Time:
function Format () : String
{
var seconds : int = Time.timeSinceLevelLoad;
var minutes : int = seconds / 60 % 60;
var centiseconds : int = (Time.timeSinceLevelLoad - seconds) * 100;
var timeStr : String;
seconds = seconds % 60;
timeStr = minutes.ToString ("D2") + ":" + seconds.ToString ("D2") + ":" + centiseconds.ToString ("D2");
guiText.text = timeStr;
}
NOTE: In the above code, there are a few lines that I excluded. The lines I excluded just reset the Current Time when the level is reset (when the Fish hits a Hook).
Now obviously, the code to change the Best Time would have to go into the collision function for my fish, but I just wanted to show how I set up the current time, if it makes any difference.
I’m pretty sure that this much information should make things rather clear, if not already!
– Chris
- You answered this yourself.
- You’ll need to expose your seconds, minutes and centiseconds variables, as well as have an equal set for best time. Alternatively, you could remove the semicolons from your time strings, and check the numbers against each other.
- My first post answers this.
Based on your WIP thread it seems like you’re still having some trouble with this so let me see if I can clarify the answer that @Beennn1 has provided. The function you call when the fish dies should look something like this…
private void onGameOver(){
//get the score for this round
float currentScore = Time.timeSinceLevelLoad;
//get the stored high score, this will be 0 by default
float bestScore = PlayerPrefs.GetFloat("highscore");
if(currentScore>bestScore){
//store the current score as the new high score
PlayerPrefs.SetFloat("highscore",currentScore);
//this function (written elsewhere) could display a new high score message or whatever you want.
onNewHighScore();
}
}
Also as a separate piece of advice, regarding your Format() function you should not put the formatting of the score and the displaying of the score in the same function. Instead have a “private string formatScore(float score)” function that takes in the score and returns a formatted string. Then you can call guiText.text = formatScore(Time.timeSinceLevelLoad);
Thanks! I will try this out! As of now, I have the highscore counting up when you beat it, but I just don’t know how to save it.
- Chris
it’s worth mentioning that my provided code was in c# and written in my web browser so probably won’t work copy/pasted. It’s just meant to give you an idea
Looking at it now, I realize it isn’t Javascript! Just a few minor changes and it will work in Unity.
- Chris