c# - 尝试添加分数钱或水时出错

标签 c# unity3d

我尝试在 unity3d c# 中实现一个评分系统,但似乎什么也没发生 我在对象上有对撞机元素并且处于事件状态 但是当玩家碰撞时它会破坏物体但它不会增加分数金钱和水

这是触发代码

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class waterpicks : MonoBehaviour {

void  OnTriggerEnter (  Collider other   ){
    if (other.tag == "water_bottle1") {
        scoreManager.money += 10;
        scoreManager.score += 10;
        scoreManager.water += 1;

        Destroy(other.gameObject);
    }

}
}

这是我的评分系统

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class scoreManager : MonoBehaviour {

public static int score;
public static int money;
public static int level;
public static int water;
public static int drinks;
public Text ShowScore;
public Text Showmoney;
public Text Showlevel;
public Text Showwater;

//public static int frutoscoleta;
// Use this for initialization
void Start () {
    score = 0;
    money = 0;
    level = 0;
    water = 80;
    //PlayerPrefs.GetInt ("scorePref");
    //score = PlayerPrefs.GetInt ("scorePref");
    //PlayerPrefs.GetInt ("moneyPref");
    //money = PlayerPrefs.GetInt ("moneyPref");
    //PlayerPrefs.GetInt ("levelPref");
    //level = PlayerPrefs.GetInt ("levelPref");
    ShowScore.text = "Score : " + score;
    Showmoney.text = "Money : " + money;
    Showlevel.text = "Level : " + level;
    Showwater.text = "Drinks : " + drinks;
 }



// Update is called once per frame
void Update () {
    //PlayerPrefs.SetInt ("scorePref", score);
    //PlayerPrefs.SetInt ("moneyPref", money);
    //PlayerPrefs.SetInt ("level", level);
    //scoreManager.score++;
    //scoreManager.money++;
    //scoreManager.level++;

}


}

最佳答案

代码在Start 函数中运行。 Start 函数只运行一次。 Update 函数在每一帧运行,您的增量代码应该在 Start 函数之外运行。

Update 函数中更新 Text 组件不是一个好主意,因为您执行的字符串连接很少。将所有增量代码(例如 scoreManager.money += 10scoreManager.score += 10; 替换为一个函数,然后更新 Text 组件在每个函数中,Text 组件在相应变量更改时更新。

public class Waterpicks : MonoBehaviour
{
    ScoreManager scoreManager;

    void Start()
    {
        GameObject obj = GameObject.Find("ScoreMangerHolder");
        scoreManager = obj.GetComponent<ScoreManager>();
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "water_bottle1")
        {
            scoreManager.incrementMoney(10);
            scoreManager.incrementScore(10);
            scoreManager.incrementWater(1);

            Destroy(other.gameObject);
        }
    }
}

评分系统

public class ScoreManager : MonoBehaviour
{

    public static int score;
    public static int money;
    public static int level;
    public static int water;
    public static int drinks;
    public Text ShowScore;
    public Text Showmoney;
    public Text Showlevel;
    public Text Showwater;

    //public static int frutoscoleta;
    // Use this for initialization
    void Start()
    {
        score = 0;
        money = 0;
        level = 0;
        water = 80;

        ShowScore.text = "Score : " + score;
        Showmoney.text = "Money : " + money;
        Showlevel.text = "Level : " + level;
        Showwater.text = "Drinks : " + drinks;
    }

    public void incrementMoney(int amount)
    {
        money += amount;
        Showmoney.text = "Money : " + money;
    }

    public void incrementScore(int amount)
    {
        score += amount;
        ShowScore.text = "Score : " + score;
    }

    public void incrementWater(int amount)
    {
        water += amount;
        Showwater.text = "Water : " + water;
    }
}

这向您展示了您的代码应该是什么样子。注意类名的大写。我认为你应该这样做。您可以继续添加其他功能来更新我没有包含的文本。您还可以将 incrementXXX 函数设为 static 并直接调用它,但这不是必需的。

关于c# - 尝试添加分数钱或水时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45896943/

相关文章:

c# - 字典和锁 C# 的奇怪问题

c# - 如何在 GridView 列中使用复选框存储库添加文本?

unity3d - Unity Json.net System.Reflection.Emit 在 iOS 中的错误

android - 新的 Unity 应用内购买集成 (Android)

unity3d - 如何自动从 Assets 商店重新下载 Unity 依赖项?

c# - telerik radgrid 控制详细信息屏幕在单击主记录时不刷新

c# - SandCaSTLe HTML 输出多个命名空间

c# - 了解 C# 中的事件和事件处理程序

android - 统一开发构建有什么用(android)

c# - 当前上下文中不存在“CrossPlatformInputManager”