这个脚本在玩家身上,当被他触摸时,会销毁硬币。
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);
}
}
也许试着把两个物体送到虚空?