enemyHealth中的update方法希望我将targetHealth设为静态的,但如果我这样做了,我将无法创建唯一的敌人。
{
public Text enemyHealth;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
enemyHealth.text = EnemyVitals.targetHealth.ToString();
}
}{
public double targetHealth = 100;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (targetHealth <= 1)
{
Destroy(gameObject);
}
}
}发布于 2020-04-16 16:08:35
据我所知,这是:enemyHealth.text = EnemyVitals.targetHealth.ToString();试图访问targetHealth,就像您访问静态类一样,例如Vector3.up。这些静态类/方法允许您在不创建类的实例的情况下使用它们的方法。因此,在本例中,您需要一个对EnemyVitals实例的引用,并在该实例上调用.targetHealth。例如,您可以通过EnemyVitals enemyVit = new EnemyVitals();实例化一个字段,或者在第一个类中声明一个公共字段,如:public EnemyVitals enemyVit;,然后在编辑器中将EnemyVitals引用拖放到该字段。我认为你可能更愿意在游戏中以某种方式获得引用,例如通过光线投射到可能的敌人物体等。从你的帖子来看,在我看来,后者更像是你想要的。
https://stackoverflow.com/questions/61243001
复制相似问题