- Home /
Hit enemy life with raycast
This is part of the code of my bullet that take enemy life, but it gives me an error:
Type UnityEngine.Component' does not contain a definition for
health' and no extension method health' of type
UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)
Script:
if(Physics.Raycast(rayAim, out hit, range)){
if(hit.collider.gameObject.tag == "Enemy") {
hit.collider.GetComponent("EnemyScript").health --;
}
}
(I have no variable on bullet script, but i have health variable on enemyscript)
Answer by robhuhn · Aug 01, 2013 at 07:44 PM
Looks like c# because of the out hit
. So you should use a type safe syntax e.g.:
EnemyScript enemy = hit.collider.GetComponent<EnemyScript>();
if(enemy != null) //null check could be useful
{
enemy .health--;
}
now i have that error:
Type EnemyScript' does not contain a definition for
health' and no extension method health' of type
EnemyScript' could be found (are you missing a using directive or an assembly reference?)
health is a property of EnemyScript? $$anonymous$$ay be it's declared as private without accessors?
i've solved my problem, the problem was the variable:
public int Health;
i change to:
public int health;
and it worked, thanks bro :)