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

时间:2017-08-26 15:23:04

标签: 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++;

}


}

2 个答案:

答案 0 :(得分:4)

代码在Start函数中运行。 Start函数仅运行一次。 Update函数每帧运行,增量代码应在Start函数外运行。

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

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并直接调用,但这不是必需的。

答案 1 :(得分:2)

你需要&#34;显示&#34;开始之外的文本,在更新中是一个选项...

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;     
     }      

    // Update is called once per frame
    void Update () {            
        ShowScore.text = "Score : " + score;
        Showmoney.text = "Money : " + money;
        Showlevel.text = "Level : " + level;
        Showwater.text = "Drinks : " + drinks;
    }       
}