I am new to unity and I am developing this android 2D game where my character moves above and as per the object moves above the score keeps on getting increased till the character collides with any object. I am able to show the score ( Current score) and also I guess I am able to store the highest score but I am not able to display the stored high score.
I want to display the high score along with the current score. Hope you can understand.
This is the code I created
#pragma strict
private var highestScore : int = 0;
private var currentScore : int;
private var dummyScore : int;
var scoreText : UI.Text;
function Start()
{
dummyScore = Time.time;
}
function Update()
{
if (scoreText.name == "ScoreText")
{
scoreText.text = ""+currentScore;
}
currentScore = (Time.time-dummyScore)*0.5; // to add on to current score
print("Score = "+currentScore);
scoringFunction();
}
function scoringFunction()
{
if(currentScore>highestScore)
{
highestScore = currentScore;
PlayerPrefs.SetInt("PrefScore",highestScore);
PlayerPrefs.Save();
print("Highest Score Saved"+highestScore);
}
}
now it is showing my current score which is getting increased but when my character collides it doesnt show the high score. Some one please help me. Trying to figure out from many hours now.
firstly you’re going to need something to track “we’re running” and “we’ve crashed”, given that it seems to be only those two options a boolean would make sense.
you’ll need to create function(s) which can set that boolean which can be called when you start running and when you collide.
this could be done in a game manager script (if you have one) and referenced in the score script or if the game you are making is simple enough you could run it from the score script.
once you have that control you need an if…else in update which can show one thing when you are running and something else when you’ve crashed based on that boolean.
that is a really good logic and thanks a lot for your answer. Meanwhile can you take a look at the new code I came up with which I believe works halfway but comes up with a small problem. Actually the highscore is displaying the same value as the current score , it doesn’t display the last highest current score. Hope you can understand. Here is the piece of code of both score system and collision.
#pragma strict
var highestScore : int; // to display the highest scores
private var currentScore : int; //to display the current ongoing score
var scoreText : UI.Text;
var score : int;
var scoreText2 : UI.Text;
function Start()
{
currentScore = Time.time; // to keep current score to go on the basis of Time.time
score = PlayerPrefs.GetInt("currentScore");
}
function Update()
{
if (scoreText.name == "ScoreText") // if the gui text matches
{
score = (Time.time-currentScore)*0.5;
scoreText.text = "Current Score = "+score;
}
if(scoreText2.name =="ScoreText2")
{
scoreText2.text="Highest Score = "+highestScore;
}
if(score>highestScore)
{
highestScore = score;
PlayerPrefs.SetInt("PrefScore",highestScore);
PlayerPrefs.Save();
}
if(highestScore>score)
{
//highestScore = score;
print("high Score Saved"+score);
}
//print(highestScore);
}
Below is the code for the collision of the player where the level restarts when the player collides . Should I include the score saving script here so that it saves the score when collides. Or is it a bad idea.? Below is the collision script.
#pragma strict
function Start () {
}
function Update () {
}
// Collision function goes here.
function OnCollisionEnter2D()
{
//Destroy(gameObject);
this.GetComponent.<Renderer>().enabled = false;
waitFor();
}
function waitFor(){
yield WaitForSeconds(0.2);
Application.LoadLevel(0);
}
Thanks for the reply. Learned something new . I cleaned up the if statements and nothing went wrong. However I still cannot figure out why my highscore is not getting saved and why my highscore and current score is same .
That is the code you see and there is nothing else I wrote. I guess I am loading it from the start function. Right? Can you tell me how will I load the highscore when the game starts.