我编写了一个愚蠢的游戏,我没有任何目的可以在C#中进行编码练习,但即便是这个简单的if语句也让我感到难过。是的,我知道,有点愚蠢,但我一直在寻找15分钟并决定在这里发帖。我制作了一个Health变量并告诉游戏在健康状态为或低于零时摧毁游戏对象,然而,即使这似乎也不起作用。我将假设我缺少某种分号。我们会看到。
我已经尝试绕过Dead();函数,只是说如果生命值为0或更低,破坏gameObject,甚至不起作用。
void Dead()
{
Destroy(gameObject);
}
void Update()
{
if (Health <= 0)
{
Dead();
}
}
void ApplyDamage(int TheDamage)
{
Health -= TheDamage;
}
}
优选地,我希望游戏在0或更低时摧毁敌人。
试试这个:
private int _health = 5;
private void Update()
{
// If the health is under or equals to 0, die
if(_health <= 0)
{
Die();
}
}
// Kills the gameObject
private void Die()
{
Destroy(gameObject);
}
// Make sure you call this function
// You call this funtion how you are suppose to, and give it the parameter *ApplyDamage(10)*
public int ApplyDamage(int amount)
{
return _health -= amount;
}