Don't know how to get enemy's health

前端 未结 3 1817
遇见更好的自我
遇见更好的自我 2021-01-07 02:49

I have this code and I don\'t know why hit.collider.gameObject.GetComponent(\"health\") is returning null

void Shoot() {
        Vector2 mousePo         


        
3条回答
  •  孤独总比滥情好
    2021-01-07 03:30

    You are using Unity are you not? It looks that way from the code you provided. GetComponent() is a method that returns a reference to a component of the game object. These are the things you drag onto a game object in the Editor. Things like Box-Colliders and Rigidbodys. The code you wrote will not return anything because there is no game component in Unity called "health". To get the health of the enemy,you need to set up a reference to the script controlling the enemy and get its health value from there.

提交回复
热议问题