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.
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
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");
}
}