gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:17:13 26 4
gpt4 key购买 nike

我尝试在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com