Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411490 Posts in 69371 Topics- by 58428 Members - Latest Member: shelton786

April 25, 2024, 02:54:52 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)My high score do not update after restart
Pages: [1]
Print
Author Topic: My high score do not update after restart  (Read 643 times)
MNQ
Level 0
*


View Profile
« on: November 06, 2014, 11:07:33 PM »

hello, i got a problem here. I'm developing shooting game. Then, i need to display the high score but it's doesn't shown. What should i do? I can't figure out the problem right now.This is android game.

here my health script (attached to every enemy)

Code:
using UnityEngine;

public class HealthScript : MonoBehaviour
{

    public static HealthScript instance;
    public int hp = 1;
    private GUIText scoreReference;
    private GUIText highscoreReference;
    private static int _highscore = -1;
    public int highscore {
        get { if (_highscore == -1)
            _highscore = PlayerPrefs.GetInt("Highscore", 0);
            return _highscore;
        }
        set {
            if (value > _highscore) {
                _highscore = value;
                highscoreReference.text = _highscore.ToString();
                PlayerPrefs.SetInt("Highscore", _highscore);
            }
        }
    }

    public bool isEnemy = true;


    private static int points;
    public void Damage(int damageCount) {
        hp -= damageCount;

        if (hp <= 0)
        {
            // Dead!
            Destroy(gameObject);
            points++;
            scoreReference.text = points.ToString();
        }
    }

    public void gameEnd() {

        points = highscore;
        points = 0;
    }

    //update from previous code
    void Start()
    {

    scoreReference = GameObject.Find("Score").guiText;
    highscoreReference = GameObject.Find("HighScore").guiText;
    scoreReference.text = points.ToString();
    highscoreReference.text = highscore.ToString ();
    instance = this;

}


then this is my player script where i put the game over method

Code:
void OnDestroy()
{
    // Game Over.
    // Add the script to the parent because the current game
    // object is likely going to be destroyed immediately.
    transform.parent.gameObject.AddComponent ();
    HealthScript.instance.gameEnd ();
}
Logged
Fallsburg
Level 10
*****


Fear the CircleCat


View Profile
« Reply #1 on: November 07, 2014, 08:17:30 AM »

Maybe I'm missing something, but it doesn't look like you ever set the highscore.  So you probably should do something like

Code:

        if (hp <= 0)
        {
            // Dead!
            Destroy(gameObject);
            highscore = ++points;
            scoreReference.text = points.ToString();
        }


Also, you probably don't want to be destroying like that, I'd really recommend object pooling instead.

Logged
MNQ
Level 0
*


View Profile
« Reply #2 on: November 07, 2014, 10:42:36 AM »

Maybe I'm missing something, but it doesn't look like you ever set the highscore.  So you probably should do something like

Code:

        if (hp <= 0)
        {
            // Dead!
            Destroy(gameObject);
            highscore = ++points;
            scoreReference.text = points.ToString();
        }




Also, you probably don't want to be destroying like that, I'd really recommend object pooling instead.



thanks btw, but i got the solution. At the method gameEnd() , the "points = highscore;" change to "highscore = points;  Smiley Smiley

Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic