I have this code and I don\'t know why hit.collider.gameObject.GetComponent(\"health\")
is returning null
void Shoot() {
Vector2 mousePo
You need to get a reference to the script attached to the Enemy. Then use that script to manipulate the health.
Find the GameObject.
GameObject g = hit.collider.gameObject;
Get the reference to the script.
EnemyAI e = g.GetComponent();
Manipulate the health.
e.health = 0f;
In one line if you want to be badass.
hit.collider.gameObject.GetComponent().health = 0.0f;
Bonus tip: health
should be private
and EnemyAI
should have a setter and a getter for that variable.