- Home /
Problem with First Person Shooter Script
Hey All! I am generating a query in which a player will fire at the enemy, upon which, for each fire the health of the enemy will be reduced by 10. In case the value of health is less than or equal to zero the enemy will be destroyed. But on compilation it gives the following error:
Assets/Scripts/Gun.js(44,67): BCE0019: 'health' is not a member of 'UnityEngine.Component'.
Funcion behind the gun:
function fire() { var range = Mathf.Infinity;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition); var hit : RaycastHit;
if(Physics.Raycast(ray , hit, range))
{
sparks.transform.position = hit.point;
if(hit.transform.gameObject.tag == "enemy")
{
hit.transform.GetComponent("enemyHealth").health -= damage;
}
}
}
Function behind the enemy:
var health : float = 100; // But the variable health is declared and also it is // being shown, for the enemy, in the inspector > script inside unity function Update() { if(health <= 0) { Destroy(gameObject); } }
Answer by Loius · Jun 29, 2012 at 02:44 PM
Health isn't a member of Component.
GetComponent( name : String ) returns a Component.
Use GetComponent(ClassName), or cast to the correct type by using (GetComponent("Class") as Class)
Your answer
Follow this Question
Related Questions
Camera Recoil,Please help me. 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Problem with shooting script 2 Answers
How to stop camera from spazzing out? 0 Answers