代码之家  ›  专栏  ›  技术社区  ›  Naffy

PlayerPref未正确存储

  •  -1
  • Naffy  · 技术社区  · 2 年前

    我是编程新手。我能够存储最高分。 现在我有一个问题,那就是它与金钱无关。 我正试图把钱存起来,这样即使游戏关闭了,也能省下来。 我看了很多关于它的视频,但我在理解上有点问题,因为它对我来说是新的东西。

    {
        public GameObject playButton;
    
        public GameObject Gameover;
    
        public GameObject ShopMenu;
    
        public Player player;
        public TMP_Text scoreText;
    
        public TMP_Text MoneyText;
        public int score;
        public int money;
        public TMP_Text highScore;
    
        private void Awake()
        {
            Application.targetFrameRate = 60;
    
            Pause();
        }
    
        public void play()
        {
            score = 0;
            scoreText.text = score.ToString();
    
            playButton.SetActive(false);
            Gameover.SetActive(false);
            ShopMenu.SetActive(false);
    
            Time.timeScale = 1f;
            player.enabled = true;
    
            pipes[] pipes = FindObjectsOfType<pipes>();
    
            for (int i = 0; i < pipes.Length; i++)
            {
                Destroy(pipes[i].gameObject);
            }
    
            highScore.text = PlayerPrefs.GetInt("HighScore", 0).ToString();
    
            if (score > PlayerPrefs.GetInt("HighScore", 0))
            {
                PlayerPrefs.SetInt("HighScore", score);
                highScore.text = score.ToString();
            }
    
            MoneyText.text = PlayerPrefs.GetInt("money").ToString();
    
            if (money != PlayerPrefs.GetInt("money"))
            {
                PlayerPrefs.SetInt("money", money);
                MoneyText.text = money.ToString();
            }
        }
    
        public void Pause()
        {
            Time.timeScale = 0f;
    
            player.enabled = false;
        }
    
        public void GameOver()
        {
            Gameover.SetActive(true);
            playButton.SetActive(true);
            ShopMenu.SetActive(true);
    
            highScore.text = PlayerPrefs.GetInt("HighScore", 0).ToString();
    
            if (score > PlayerPrefs.GetInt("HighScore", 0))
            {
                PlayerPrefs.SetInt("HighScore", score);
                highScore.text = score.ToString();
            }
    
            MoneyText.text = PlayerPrefs.GetInt("money").ToString();
    
            if (money != PlayerPrefs.GetInt("money"))
            {
                PlayerPrefs.SetInt("money", money);
                MoneyText.text = money.ToString();
            }
    
            Pause();
        }
    
        public void IncreaseScore()
        {
            score++;
            scoreText.text = score.ToString();
            money++;
        }
    }
    
    1 回复  |  直到 2 年前
        1
  •  1
  •   Morion    2 年前

    看起来你的 money 值始终为 0 因为您没有初始化它。同样的 score 价值尝试添加

    money = PlayerPrefs.GetInt("money", 0);
    score = PlayerPrefs.GetInt("HighScore", 0);
    

    到您的 Awake() 方法