Hi lads, I’m new to Unity. My program is a FPS game. I have a enemy prefab and this is the js code:
var bloodOut : GameObject;
static var enemyHP : float = 100;
function Start () {
Target = Camera.main.transform;
}
function Update () {
if(enemyHP <= 0){
Destroy(gameObject);
}
}
I use ray-cast to detect if hits the enemy. Here is the code to generate the prefab:
function Start () {
enemy = Instantiate(enemy, Vector3(75.47046, 10.30222, 63.87109), Quaternion.identity);
//enemy = Instantiate(enemy, Vector3(85.47046, 10.30222, 63.87109), Quaternion.identity);
}
My enemy prefabs have a tag name “enemy”.
if(bulletHole1 && hit.transform.tag == "enemy"){
Instantiate(bloodSplat, hit.point + (hit.normal * frontHit), Quaternion.LookRotation(hit.normal));
enemy.enemyHP -= 5;
}
This is all working good.
BUT,
when I release the comment to generate another enemy prefab, the problem comes. If I shoot one prefab then all prefabs dead. I know it’s because the HP variable is 0. But how to fix this problem? How can I separate the HP variable of all prefabs? I’m new to Unity, please tell me a little bit more details. Thanks!