- Home /
Question by
Hex2013 · Nov 06, 2014 at 10:21 PM ·
javascriptplayerprefsscoreguitexthighscore
using playerprefs to update a guitext and record highscores
i have been working on a project where you tap spots to score points and keep going. i am having trouble determining how to use playerprefs to recored how many were tapped.
here is the script for the spots.
function Update () {
for(var i:int = 0; i < Input.touches.Length; i++)//How many touches do we have?
{
var touch:Touch = Input.touches[i];//The touch
var ray:Ray = Camera.main.ScreenPointToRay(touch.position);
var hit:RaycastHit = new RaycastHit();
if(Physics.Raycast(ray,hit, 1000))
{
if(hit.collider.gameObject == this.gameObject)
{
switch(touch.phase)
{
case TouchPhase.Began://if the touch begins
DestroyObject(gameObject);
break;
}
}
}
}
}
thanks in advance. :)
Comment
Answer by ExtremePowers · Nov 06, 2014 at 11:29 PM
To get/set highscore:
var Score : int;
function Save() {
if (PlayerPrefs.HasKey("Highscore")) {
if (PlayerPrefs.GetInt("Highscore") < Score) {
PlayerPrefs.SetInt("Highscore", Score);
}
} else {
PlayerPrefs.SetInt("Highscore", Score);
}
PlayerPrefs.Save();
}
function Load() {
if (PlayerPrefs.HasKey("Highscore")) {
Score = PlayerPrefs.GetInt("Highscore");
}
}
Just change this:
case TouchPhase.Began://if the touch begins
DestroyObject(gameObject);
To this
case TouchPhase.Began://if the touch begins
DestroyObject(gameObject);
Score++;
when i tap tap the spots, it just disappears and doesn't add to the score to the overall score, can you fix it, i a noob to coding.
sorry