- Home /
Score & High Score logic doesn't work
hello again!
i m making a memory card game. Every time i find a pair of cards i get points. When i can't find all the pairs in the game then a game over shows up and takes me to score scene. Now, in the score scene i have 2 GUIText objects on Hierarchy (points & high score) and 2 js files for them too.
I attach the points.js to points GUIText:
public static var main_points:int;
function Start () {
if(Score_Manager_1.scoreFlag == true){
guiText.text = "" + Score_Manager_1.points_;
main_points = Score_Manager_1.points_;
}
}
And then i attach the highScore.js to high score GUIText:
public var newHighScore:int;
function Update () {
newHighScore = Score_Manager.main_points;
if(Score_Manager_1.points_ >= newHighScore ){
guiText.text = "" + Score_Manager_1.points_;
}
}
The logic of it seems fine, but: Everytime i gain the first points i can see them both as my collected points (point GUIText) and as my new high score (high score GUIText) too. That's obvious at the first time cause there is no previous high score to compare it with. But then later when i play again my scene and gain less points this time, my points show the right points i collected but the high score GUIText shows the same with points GUIText. Normally high score would have displayed the last high score i made.
What's the problem here? Thanx in advance!
Answer by EX_Darius · Dec 14, 2013 at 01:12 AM
this is where I think you went wrong:
newHighScore = Score_Manager.main_points;
When writing this in your update it constantly gives "newHighScore" the value of score_manager.main_points which is Score_Manager_1.points_ .
you could try changing your update to this:
function Update () {
if(Score_Manager_1.points_ >= newHighScore ){
guiText.text = "" + Score_Manager_1.points_;
newHighScore = Score_Manager.main_points; // you could even change main_points to score_manager_1.points_ for effeciency
}
}
this way the highscore will ONLY be set if the points are higher than the newHighScore, instead of constantly updating the newHighScore no matter what points you have.
hope this helped!
EDIT: do realize your value get reset every time you reload the scene/game
Thanks for ur reply @EX_Darius But still i get the same results :(
EDIT: as for keeping my high score i did this (with no effect)on highScore.js:
public var newHighScore:int;
function Awake() {
guiText.text = "" + PlayerPrefs.GetInt("newHighScore"); //Returns the value corresponding to key in the preference file if it exists
}
function Update () {
if(Score_$$anonymous$$anager_1.points_ >= newHighScore ){
guiText.text = "" + Score_$$anonymous$$anager_1.points_;
newHighScore = Score_$$anonymous$$anager_1.points_;
PlayerPrefs.SetInt("newHighScore",newHighScore); // Sets the value of the preference identified by key
//PlayerPrefs.Save(); // Writes all modified preferences to disk
}
}
hmm could be a conflict with using 1 virtual int for 2 external values. have you tried making a method that returns the values(making the values private) ?
Sorry i made a mistake while i was copying the if statement above, i made couple of examples but no result yet my if statement is (still):
if(Score_$$anonymous$$anager_1.points_ >= newHighScore )
ins$$anonymous$$d of what i had before
Answer by fafase · Dec 14, 2013 at 11:48 AM
You do not need to perform the action in the update, it sounds like a waste of resources.
You should use a method or a property (which is a method).
This is attached to a GameManager object
GameManagerScript.cs
private var _highScore:int;
private var _score:int;
var highScoreGUI : GUIText; // Drag the GUIText here and below
var scoreGUI : GUIText;
public int Score
{
_score = value;
scoreGUI.text = _score.ToString();
if(_score >= _highScore){
_highScore = _score;
highScore.text = _highScore.ToString();
}
}
Now it depends on your logic for increasing the score. Considering you have your enemies getting destroyed:
private var _script:GameManagerScript;
var point:int; //Give a value via inspector
function Start(){
_script = GameObject.Find("GameManager").GetComponent(CamScript);
if(_script == null)Debug.Log("No component found");
}
function OnCollisionEnter(col:Collider){
if(col.gameObject.tag == "projectile"){
_script.Score += point;
Destroy(gameObject);
}
}
So this goes on the enemy, first it finds the camera and the script. When a collision occurs, the script checks if it is the projectile, then adds the values to Score and destroy the object. Score is the property that will do the job.
Now you should have a method on the game manager somewhere that should take care of when to end the scene. Do you have a list of enemies? Do you wait for a certain amount of enemies killed?
Considering the number of enemies:
private var _count:int = 0;
private var _maxEnemies:int = 50;
public int Score
{
_score = value;
scoreGUI.text = _score.ToString();
_count++;
if(_score >= _highScore){
_highScore = _score;
highScore.text = _highScore.ToString();
}
if(_count >=_maxEnemies)
{
PlayerPrefs.SetInt("HighScore", _highScore);
Application.LoadLevel("NextLevel");
}
}
It's a good approach @fafase but it won't work for me. I have to play with two GUITexts as well.
I edited the script so that your GUITexts are taken care of.
i tested ur approach according to what i've got on my project and still no results :(
I attached this script on $$anonymous$$ainCamera (highScore scene):
private var _highScore:int;
private var _score:int;
var highScoreGUI : GUIText;
var scoreGUI : GUIText;
function Awake(){
highScoreGUI.text = "" + PlayerPrefs.GetInt("_highScore");
}
function Start () {
_score = Score_$$anonymous$$anager_1.points_;
scoreGUI.text = _score.ToString();
if(_score >= _highScore){
_highScore = _score;
highScoreGUI.text = _highScore.ToString();
}
PlayerPrefs.SetInt("_highScore",_highScore);
}
youre resetting the highscore in your start function.. every time you start the game, it sets the playerpref _highscore to
the default value. remove that line of code.
nop still no results :(
with PlayerPrefs.SetInt("_highScore",_highScore); on my script i mean to save the last value of _highScore so i can generate it back to the scene when i load it
the above codes look so logically right i don't know why they don't work for me though :((