- Home /
Score system is not working properly, help?
I am making an infinite runner game. In the game I have the score for the current run and the highscore.
The game is setup so that I spawn enemies on the right side. My character is on the left side. Both are sprites and they "spawn" Prefabs. The character spawns a shooting ball and the spawn on the right side spawn enemies. On the enemy prefab I have a script that check to see if the shooting ball that the character shot hit it. Once that happen I add 1 point to the score. If the score is higher than any other run before it should become the highscore.
The script to look for the hit between shots and enemies work. But the score system is not working.
I am using this script at the moment:
static var score : int = 0;
PlayerPrefs.SetInt("currentScore", 0);
PlayerPrefs.SetString("PlayerScore", "Your Score: ");
PlayerPrefs.Save();
function OnTriggerEnter2D(obj : Collider2D) {
score += 1;
if ( score > PlayerPrefs.GetInt("currentScore") {
PlayerPrefs.SetInt("currentScore", score);
}
}
If the enemy hits the character it is game over and you come to another scene. On this scene I have a PLAY button that takes you back to the PlayScene. But once you start shooting enemies your score just adds upon the old score. It doen´t reset. I have tried to add reset(score=0;) in the Awake function but that makes the score 0 for each enemy that spawns.
For the game over scene I have this script attached to the PLAY button: Application.LoadLevel("MainScene");
I am really confused and can´t find what the issue is. I´ve been trying to wrap my head around this problem for way to many hours now.
Can someone please help me?
Is that literally your code? Does the PlayerPrefs calls actually live outside of a function? or did you remove the other code? Are you using PlayerPrefs.GetInt("currentScore") anywhere? Are you marking any objects as DontDestroyOnLoad?
good point Larden, also, don't use PlayerPrefs so extensively. PlayerPrefs are meant to carry the data between the game launches, not during playing because they store everything on HD and reading/writing from HD is a slow process. create a separate game object to store the score.
Hmm.. How do I do so? I am new to unity and JS and C# so do not know how I can make a gameObject store my value.
Thank you all of the inputs. With your help and suggestions I managed to get it to work. I really appreciate your help!
Good luck coding!
Answer by gameplay4all · Dec 31, 2014 at 09:57 AM
You should indeed not reset the player prefs each time in the awake and as you've done in the 2nd and 3rd lines.
You should also call PlayerPrefs.Save();
in OnTriggerEnter2D
.
I also don't get why you would save a string as standard as "Your score" in player preferences, but that is however irrelevant to the question.
Good luck!
Answer by MiraiTunga · Dec 31, 2014 at 10:34 AM
Dont make score Static , does it have to be static ? , use
`getComponent<theScoreScript>().score=something;`
If the Score is not static, the score does not update when the shot hits the enemy.
Answer by webe · Dec 31, 2014 at 01:31 PM
This is what I used for my infinite sidescroller. I removed a couple of functions that get/set/print the values. In your case you might want to check for collision elsewhere, and if the colliding object is the Player, call the AddPoint() function. Hope it helps.
int score = 0;
int highScore = 0;
void Start () {
highScore = PlayerPrefs.GetInt("highScore", 0);
}
public void AddPoint(){
score++;
if (score > highScore) {
highScore = score;
}
}
void OnDestroy(){
PlayerPrefs.SetInt ("highScore", highScore);
}
shouldnt this line of code read
`highScore = PlayerPrefs.GetInt("highScore");`
not
highScore = PlayerPrefs.GetInt("highScore", 0);
The 0 is the default value, which is used if nothing is found (first run for example)