- Home /
Why is my highScore counting from 10 , not from 0 ??? Thanks in advance :))
using System.Collections; using UnityEngine;
public class idemmosc : MonoBehaviour {
public float speed = 4f;
public Rigidbody2D rb;
private float movement = 3f;
static int score = 0;
static int hiScore = 0;
public GUIText text;
static public void AddPoint()
{
score++;
if (score > hiScore)
{
hiScore = score;
}
}
void Start()
{
score = 0;
hiScore = PlayerPrefs.GetInt("hiScore", 0);
text.text = "Score" + "\nHiScore" ;
}
void Update()
{
movement = Input.GetAxis("Horizontal") * speed;
}
void FixedUpdate()
{
rb.MovePosition(rb.position + new Vector2(movement * Time.fixedDeltaTime, 0f));
}
void OnCollisionEnter2D(Collision2D other)
{
PlayerPrefs.SetInt("hiScore", hiScore);
if (other.gameObject.name == "Ball")
text.text = "Score" + score + "\nHiScore" + hiScore;
AddPoint();
}
}
Comment
Please use the code formatting button ("101 010" above the text-input-field) correctly so the whole code is formatted correctly. Thanks!
Best Answer
Answer by jwulf · Feb 21, 2017 at 12:46 PM
Possibly because 10 is the last stored value in PlayerPrefs?
In Start() you fetch hiScore from PlayerPrefs:
hiScore = PlayerPrefs.GetInt("hiScore", 0);
If that value is 10, counting starts at 10 of course.
If you want to reset your Highscore, you need to reset it in the PlayerPrefs, i.e.
hiScore = PlayerPrefs.SetInt("hiScore", 0);