- Home /
Question by
batopaa · May 27, 2015 at 10:59 AM ·
javascriptplayerprefsscore systemhighscoregui.box
Im Doing HighScore System To My Game And There Is Something Wrong In My Scripts
So Im Doing highScore System When Players Health Is 0 It Sets New highscore But There Is Problem That When Helath Is 0 Nothing Happens
-------HealthScript--------
var curHealth : int = 100;
function Start(){
}
function OnGUI(){
GUI.Box(Rect(Screen.width*0.9,Screen.height*0.5,100,25), curHealth.ToString());
}
function OnTriggerEnter (other : Collider) {
if(other.gameObject.tag == "enemy")
curHealth -= 10;
}
-------ScoreScript--------
#pragma strict
var highScore: int = PlayerPrefs.GetInt("HighScore");
function Start(){
InvokeRepeating("EachSecond",1.0,1.0);
}
var Elkit : GameObject;
var score: int = 0;
var addscore = 7;
var HighScore : int;
function EachSecond()
{
score=score+addscore;
}
function OnGUI(){
GUI.Box(Rect(Screen.width*0.9,Screen.height*0.2,100,25), score.ToString());
var health : Health;
health = Elkit.GetComponent("Health");
if(health.curHealth == 0 && score <= highScore)
{
PlayerPrefs.SetInt("HighScore",score);
}
}
---------HighScoreScript-----------
#pragma strict
var highScore: int = PlayerPrefs.GetInt("HighScore");
function Start () {
}
function OnGUI(){
GUI.Box(Rect(Screen.width*0.9,Screen.height*0.3,100,25), highScore.ToString());
}
Comment
Add that condition in Update() and for checking new score as highscore you will have condition like score > highscore then score value will be set as a new high score.
Thanks.
Also, with your HighScoreScript you are only "getting" the high score value one time. If you put a PlayerPrefs.GetInt("HighScore"); in the OnGUI() function before the GUI.Box it should update properly as well.