- Home /
Object.Destroy destroying all prefabs
Hello-
I've been working on a script for a 2D game I've been working on, but when I call the destroy function, it destroys all of the prefabs, not just the one want destroyed. The script is below. Thanks!
#pragma strict
var detonationsprite1: GameObject;
var detonationsprite2: GameObject;
var detonationsprite3: GameObject;
var detonationsprite4: GameObject;
var detonationsprite5: GameObject;
var detonationsprite6: GameObject;
var spritechooser: int;
static var score: int;
function Update () {
print(score);
//for debugging purposes only
if (Input.GetButton ("Fire1")){
TapFunction();
//initialize tapfunction
}
}
function TapFunction(){
spritechooser = Random.Range(1,6); // Randomly selects a detonationsprite to use
if(spritechooser == 1){
Instantiate(detonationsprite1, transform.position, transform.rotation);
}
if(spritechooser == 2){
Instantiate(detonationsprite2, transform.position, transform.rotation);
}
if(spritechooser == 3){
Instantiate(detonationsprite3, transform.position, transform.rotation);
}
if(spritechooser == 4) {
Instantiate(detonationsprite4, transform.position, transform.rotation);
}
if(spritechooser == 5){
Instantiate(detonationsprite5, transform.position, transform.rotation);
}
if(spritechooser == 6){
Instantiate(detonationsprite6, transform.position, transform.rotation);
}
Destroy (gameObject); // Delete Brainmonster sprite
score += 1; // Tally score
}
Comment
Answer by Jeff-Kesselman · Jun 26, 2014 at 04:39 PM
I would guess you have this script on your prefab.
Therefor, all instances have it.
Therefor all instances respond to the key press and destroy themselves.