【问题标题】:Unity score text gets upadated inside unity editor but does not get updated in a real deviceUnity 分数文本在 Unity 编辑器中更新,但不会在真实设备中更新
【发布时间】:2016-11-29 05:12:33
【问题描述】:
using UnityEngine;
using System.Collections;

public class Scoremanager : MonoBehaviour {
    public static Scoremanager instance;
    public int Score;
    void Awake(){
        if (instance == null) {
            instance = this;
        }

    }
    // Use this for initialization
    void Start () {
        Score = 0;
        PlayerPrefs.SetInt ("Score", 0);
    }

    // Update is called once per frame
    void Update () {

    }
     public void IncrementScore(){
        Score++;

    }
    public void StopScore(){
        PlayerPrefs.SetInt ("HighScore", Score);
        if(PlayerPrefs.HasKey("HighScore")){

            if (Score > PlayerPrefs.GetInt ("HighScore")) {
                PlayerPrefs.SetInt ("HighScore", Score);

            }
        }
        else{
            PlayerPrefs.SetInt ("HighScore", Score);
    }
}

游戏在统一编辑器游戏视图中运行良好,并且分数文本得到更新,但是当我在真实设备中安装游戏时,分数文本没有得到更新。我哪里错了?

【问题讨论】:

  • 什么叫IncrementScore?您发布的代码看起来甚至无法编译,您的大括号已经过时了。
  • 增量分数是从另一个脚本调用的。
  • 您仍然缺少右大括号,并且您还没有回答重要的问题,这些函数是如何调用的?
  • 增量分数是从另一个脚本调用的。当我在统一游戏视图中运行游戏时,分数会很好地增加,但是当我在真实设备中运行游戏时,分数不会增加。

标签: unity3d unity5


【解决方案1】:
public class ScoreManager : MonoBehaviour {

    public static int Score{
        get{ 
           return PlayerPrefs.GetInt ("Score", 0);
        }set{ 
           PlayerPrefs.SetInt ("Score", value);
           if (HighScore < value) {
               PlayerPrefs.SetInt ("HighScore", value);
           }
        }
    }

    public static int HighScore{
        get{ 
           return PlayerPrefs.GetInt ("HighScore", 0);
        }
    }

    void Start(){
       Score = 0;
    }
}

public class AnotherScript : MonoBehaviour {

    void IncreaseScore(){
        ScoreManager.Score++;

        Debug.Log (ScoreManager.Score);
        Debug.Log (ScoreManager.HighScore);
    }
}

有一种方法可以做到这一点,您可以轻松管理您的游戏得分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多