- Home /
How to control variable of prefab and its clone
I am working in FPS game i have created an enemy as soon as the game start 4 clone of enemy prefab will be instantiated, i have created a function hitCheck() for checking if enemy has been hit by bullet or not, i am incrementing a var hit, if hit increases 5 gameobject will get destroy, but the problem i am facing if i hit one clone var hit of all other clone will also get incremented, i want to know how to use hit variable so that hit will only increases if particular enemy get hit not all of its clone.
Here is my collisiondetection script
function OnCollisionEnter(col:Collision)
{
//print(col.gameObject.name);
if(col.gameObject.name.Contains("Enemy"))
{
col.gameObject.GetComponent(EnemyAI).hitCheck();
}
Destroy(gameObject);
}
and here is hitcheck() function
function hitCheck()
{
Debug.Log(transform.name+" "+hit);
hit++;
var cposition:Vector3;//get current position of enemy so that we can place blood at that position
cposition = Vector3(transform.position.x,transform.position.y,transform.position.z);
Instantiate(blood,cposition,Quaternion.identity);
if(hit>4)
{
Destroy(gameObject);
}
}
code looks fine. If I understand when player shoots 5 times in one enemy all of them becomes dead right?
Exactly this is what happening, i have 3 diffrent clone of enemy if i change the hit value of one clone it reflect in hit value of all other clone, For example if i hit enemy0 with 5 bullet hit value of enemy1,enemy2 and enemy3 will also become 5 and they get destroyed.
Your answer
![](https://koobas.hobune.stream/wayback/20220613141656im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Array of prefabs 0 Answers
Cast Source Instantiating Error 1 Answer
instansiating prefab, then adding force in camera direction C# 0 Answers
Cloned Object does not run Scripts 2 Answers