- Home /
why all my prefabs get destroyed whem i destroy one of them,Why all my prefabs get destroyed whem i destroy one of them
I am making a game, and the body has a separate object for the head, i wanted to make the head a weaker point, but whem i clonned my prefab and shooted one of them all the other clones got destroyed;
body: public int VidaTotal; public static int Vida;
void Start () {
Vida = VidaTotal;
}
// Update is called once per frame
void Update () {
if(Vida <= 0){
Destroy(this);
}
}
void OnTriggerEnter2D(Collider2D col){ if(col.gameObject.CompareTag("bullet")){ Vida --; Destroy(col.gameObject);
}
}
Head : void OnTriggerEnter2D(Collider2D col){ if(col.gameObject.CompareTag("bullet")){ Enemy.Vida = - 1; Destroy(col.gameObject);
},
Answer by thunderbuns · Nov 15, 2018 at 12:12 AM
Vida is declared as a static variable. Do so means that everytime an objects collides with something all instances of Vida are set to true.
Answer by arbazgillani · Nov 15, 2018 at 08:55 AM
Remove static from vida. Static is declared and can never have more than one instance. All prefabs are getting the static value. Remove static your issue will be removed.