- Home /
The question is answered, right answer was accepted
Game Over page score is always 0
I'm following Mike Geig's infinite runner tutorial but even though the scripts are identical my game over page always says 0 as the score and I don't know why.
this is the game over script where I think something might be wrong
using UnityEngine;
using System.Collections;
public class GameOverScript : MonoBehaviour {
int score = 0;
void Start () {
score = PlayerPrefs.GetInt("score");
}
void OnGUI()
{
GUI.Label(new Rect (Screen.width / 2 - 40, 50, 80 ,30), "Game Over");
GUI.Label(new Rect (Screen.width / 2 - 40, 300, 80, 30), "Score: " + score);
if(GUI.Button(new Rect (Screen.width / 2 - 30, 350, 60, 30), "Retry?"))
{
Application.LoadLevel (0);
}
}
}
You've got
PlayerPrefs.GetInt("score");
But do you ever Set it?
yes right here line 19
using UnityEngine;
using System.Collections;
public class HUDScript : $$anonymous$$onoBehaviour {
float playerScore = 0;
void Update () {
playerScore += Time.deltaTime;
}
public void IncreasedScore(int amount)
{
playerScore += amount;
}
void OnDisable()
{
PlayerPrefs.SetInt("Score", (int)playerScore * 100);
}
void OnGUI()
{
GUI.Label(new Rect(10, 10, 100, 30), "Score: " + (int) (playerScore * 100));
}
}
oh wait I figured it out, I had the get as a capital S but the set was a lowercase
Answer by Kiwasi · Aug 04, 2014 at 12:44 AM
You are getting "score" and setting "Score". Strings are case sensitive, so these two variables are different.
Follow this Question
Related Questions
Error in script I haven't even opened 0 Answers
Unity Script Editor Not Working 1 Answer
Crash on .exe but fine on editor... 1 Answer
Error with code, cannot figure out solution... 1 Answer
What's wrong with this script? 2 Answers