【问题标题】:Scoring does not work in the game on the Unity engine在 Unity 引擎上的游戏中无法进行评分
【发布时间】:2021-08-15 23:36:42
【问题描述】:

我为 android 调制 3d 游戏。并评分剂量工作。最本质的是,在地图外移除汽车后,点数、最高成绩和硬币都被钉死了。 unity不报错的编译器是什么

using UnityEngine;
using UnityEngine.UI;

public class ScoreShow : MonoBehaviour
{

    [SerializeField]
    private Text topRecord;

    void onEnable()
    {
        GetComponent<Text>().text = "Score: " + deletecar.countCars.ToString();
        if (PlayerPrefs.GetInt("Score") < deletecar.countCars)
        {
            PlayerPrefs.SetInt("Score", deletecar.countCars);
            topRecord.text = "Top:" + deletecar.countCars.ToString();
        }
        else
            topRecord.text = "Top:" + PlayerPrefs.GetInt("Score").ToString();
    }
}

代码的第 2 部分

using UnityEngine;

public class deletecar : MonoBehaviour
{

    public static int countCars;

    void Start()
    {
        countCars = 0;
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Player")
        {
            if (!Collisoinscars.lose)
            {
                countCars++;
                PlayerPrefs.SetInt("Coins", PlayerPrefs.GetInt("Coins") + 1);
                Destroy(other.gameObject);
            }
        }
    }
}

【问题讨论】:

    标签: c# visual-studio unity3d 3d


    【解决方案1】:

    它应该是 OnEnable() 它是一个错字。

    【讨论】:

      【解决方案2】:

      实际上,问题出在这段代码中,您在其中启用了游戏对象。

      void onEnable()
      {
          GetComponent<Text>().text = "Score: " + deletecar.countCars.ToString();
          if (PlayerPrefs.GetInt("Score") < deletecar.countCars)
          {
              PlayerPrefs.SetInt("Score", deletecar.countCars);
              topRecord.text = "Top:" + deletecar.countCars.ToString();
          }
          else
              topRecord.text = "Top:" + PlayerPrefs.GetInt("Score").ToString();
      }
      

      这是一个拼写错误,void onEnable 将是 OnEnable

      【讨论】:

      • 用户在拼写错误时会收到什么错误信息?
      猜你喜欢
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多