[英]Having an issue with Unity(C#) my score counter is also adding +1 when i hit a different object (my +1HP object)
这个脚本在玩家身上,当被他触碰时销毁硬币。
private void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.CompareTag("Coins"))
{
AudioSource.PlayClipAtPoint(coinSound, transform.position);
Destroy(other.gameObject);
}
}
此脚本用于 HP up 对象,当玩家触摸时会增加 +1hp。 ScoreManager
- 更改写入的分数,以便玩家可以看到它已更新。
public class ScoreManager : MonoBehaviour
{
public static ScoreManager instance;
int score = -1;
public Text text;
// Start is called before the first frame update
void Start()
{
if(instance == null)
{
instance = this;
}
}
// Ao tocar numa moeda, é acrescentado o valor "x" ao Score, o valor x pode ser mudado manualmente no Unity, podendo utilizar o mesmo script para moedas de diferente valor;
public void ChangeScore(int coinValue)
{
score = score + coinValue;
text.text = "x" + score.ToString();
}
}
CoinSystem - 当玩家接触硬币时,分数增加 +1。
public class CoinSystem : MonoBehaviour
{
public int coinValue = 1;
// Atualiza o Score no canvas
private void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.CompareTag("Player"));
{
ScoreManager.instance.ChangeScore(coinValue);
}
}
}
我认为您的问题至少有一部分是在您的CoinSystem
类中的 if 语句之后有一个分号 ( ; )。
我建议注意 Visual Studio 的警告。 它警告您可能存在错误的空语句,如绿色波浪线所示。
private void OnTriggerEnter2D(Collider2D Object1, Collider2D Object2){
if(Object1.gameObject.CompareTag("Player") && Object2.GameObject.CompareTag("Coin")){
ScoreManager.instance.ChangeScore(coinValue);
}
}
也许尝试向虚空发送两个对象?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.