Hi, I have problem with PlayerPrefs. I as U see in full code (last code) I use
if (points > highscore)
{
PlayerPrefs.SetInt("Highscore", points);
Debug.Log("New highscore: " + PlayerPrefs.GetInt("HighScore"));
}
but it just say 0 no matter how many points I will get. Debug.Log is next to the SetInt SO… SetInt write 0 to PlayerPrefs “Highscore”, but why? There are in if with points > highscore that is same to PlayerPrefs “Highscore” because I typed
void Start()
{
highscore = PlayerPrefs.GetInt("HighScore", 0);
}
by the way it works good when I get 0 points and say nothing about beating highscore
full code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class player : MonoBehaviour {
public float mvspeed = 600f;
public int points = 0;
float movement = 0f;
public int highscore;
void Start()
{
highscore = PlayerPrefs.GetInt("HighScore", 0);
}
void Update () {
movement = Input.GetAxisRaw("Horizontal");
}
private void FixedUpdate()
{
transform.RotateAround(Vector3.zero, Vector3.forward, movement * Time.fixedDeltaTime * -mvspeed);
}
void OnTriggerEnter2D(Collider2D otherCollider)
{
if(otherCollider.name == "HexagonCollider")
{
Debug.Log("Score:" + points);
Debug.Log("Highscore:" + highscore);
if (points > highscore)
{
PlayerPrefs.SetInt("Highscore", points);
Debug.Log("New highscore: " + PlayerPrefs.GetInt("HighScore"));
}
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
if(otherCollider.name == "PointCollider")
{
points += 1;
}
}
}