I’m doing an infinite runner type game for android, I got 3 scenes and I need a highscore system.
I would figure it on my own but I just started with programming a few weaks ago and I need this game finnished soon.
My first scene:
Here I need to call the saved highest score and displayed it here
2nd (game) scene:
Here I need an updated score by deltaTime during the game until the player is destroyed/collided
End scene:
Here I need to show the current score that the player achieved.
And here is my script so far, I’m having problem setting it up and updating the text correctly:
public class ScoreScript : MonoBehaviour {
static float score = 0; // there's no need of static variables
static float highscore = 0;
void Start ()
{
highscore = PlayerPrefs.GetFloat("highscore");
}
void Update ()
{
score += Time.deltaTime * 10;
if (score > highscore) // there's no need of checking this every frame
highscore = score; // there's no need of assigning a highscore every frame
}
void OnDestroy()
{
PlayerPrefs.SetFloat("score", Time.deltaTime * 10); // you are setting the score as delta time ( time between frames) when this object is destroyed
}
}
I’m not really into 2D so i can’t really help you there.
Anyways, if i can, i think you should concentrate more on unity and coding basics before getting yourself into a project. It’s fine if you’re doing this for practice
This is for practice but I have a very strong reason why I need this finished soon, basically I need to have an app in my account.
Anyway, the score system is now semi-working, few things need to be fixed:
The time should be displayed as an integer, without decimal points
The score is calculated/running at all scenes, not just the 2nd game scene, maybe I need to implement this somewhere:
Time.timeSinceLevelLoad.ToString()
Changing gui text real time I get this error: the variable scoreText hasnt been asigned.
I guess I have to drag the text I want to change into this script but for some reason I cant
Hi, to display only integers try to use ToString(“0”) instead, even if i’m not sure this will work
public GUIText scoreText;
void Update()
{
score=Time.deltaTime*10;
scoreText.text=score.ToString("0");
}
The score is running in every scene where the script is attached to an object, so you should attach it only to your player in the playing scene. This is the full script
As for the 3rd point, you must create a GUIText ( Game Object > Create other > GUIText ) , position it as you wish in the scene, and finally drag it from the hierarchy to the script in the inspector.
Thanks bbQ for helping, you helped me a ton. The score system is finally working, I had to make some changes to the script because I’m using unity 4.6 with the new UI system.
In the end I made 3 scripts, one for each scene and I’ll post them here if anyone wants to use them.
Note that you have to use using UnityEngine.UI;
First script for the 1st scene that gets and displays the high score:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class HighscoreScript : MonoBehaviour {
public float highscore = 0;
public Text hscoreText;
void Start ()
{
highscore = PlayerPrefs.GetFloat("highscore");
hscoreText.text=highscore.ToString("0");
}
}
Second script that sets the score and the high score:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ScoreScript : MonoBehaviour {
public float score = 0;
public float highscore = 0;
public Text scoreText;
void Update ()
{
score += Time.deltaTime * 10;
scoreText.text=score.ToString("0");
}
void OnDestroy()
{
PlayerPrefs.SetFloat("score", score);
if (score > highscore)
{
highscore = score;
PlayerPrefs.SetFloat("highscore", highscore);
}
}
And the final script that shows the current score at the end:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class EndscoreScript : MonoBehaviour {
public float score = 0;
public float highscore = 0;
public Text scoreText;
void Start ()
{
score = PlayerPrefs.GetFloat("score");
scoreText.text=score.ToString("0");
}
}