I’m building a app for android and i can’t get the high score to save.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CircleCollider2D))]
public class DamageEntity : MonoBehaviour {
public float damage = 15;
public bool damageOnEnter = true;
public bool damageOnExit = false;
public bool damageOnStay = true;
static int score = 0;
static int highScore = 0;
static public void AddPoint(){
if(score > highScore) {
highScore = score;
}
}
// Use this for initialization
void Start() {
}
void OnTriggerEnter2D(Collider2D otherObj) {
if (damageOnEnter) {
if (otherObj.GetComponent<BaseEntity> () != null) {
otherObj.GetComponent<BaseEntity> ().TakeDamage (damage);
score ++;
}
}
if (damageOnEnter) {
if (otherObj.GetComponent<Spawner>() != null) {
otherObj.GetComponent<Spawner>().TakeDamage(damage);
score ++;
}
}
if (damageOnEnter) {
if (otherObj.GetComponent<Spawner2>() != null) {
otherObj.GetComponent<Spawner2>().TakeDamage(damage);
score ++;
}
}
if (damageOnEnter) {
if (otherObj.GetComponent<Spawner3>() != null) {
otherObj.GetComponent<Spawner3>().TakeDamage(damage);
score ++;
}
}
}
void OnTriggerStay2D(Collider2D otherObj) {
if (damageOnStay) {
if (otherObj.GetComponent<BaseEntity> () != null) {
otherObj.GetComponent<BaseEntity> ().TakeDamage (damage * Time.deltaTime);
}
}
}
void OnGUI(){
GUI.Label (new Rect (10, 10, 100, 20), "Score" + score);
GUI.Label (new Rect (20, 20, 100, 20), "HighScore" + highScore);
}
void Update () {
highScore = score;
score = 0;
highScore = PlayerPrefs.GetInt("highScore", 0);
PlayerPrefs.SetInt("highScore", highScore);
if(score > highScore) {
highScore = score;
}
}
According to most people this should be fine for saving score but it’s not.