I want there to be a button in the game to destroy all instances of a certain coloured cube when pressed but when I use ‘FindGameObjectsWithTag()’ to try and do that it doesn’t work properly, if I press the button it will destroy any cubes that were in the game before i ran it but not any I create in the game and will only destroy red cubes (as opposed to the blue and green cubes). This is my script;
var Projectile1 : Rigidbody;
var Projectile2 : Rigidbody;
var Projectile3 : Rigidbody;
var SpawnObject : Transform;
var Speed = 30;
var FireType = 0;
var UnlockLevel : int;
function Start () {
var UnlockLevel = 0;
var redCube : GameObject[];
var blueCube : GameObject[];
var greenCube : GameObject[];
}
function Update () {
if(Input.GetButtonDown("Fire1")&&!Input.GetButtonDown("Fire2")&&UnlockLevel == 1) {
var clone : Rigidbody;
if(FireType == 0) {
clone = Instantiate(Projectile1, SpawnObject.position, SpawnObject.rotation);
}
if(FireType == 1) {
clone = Instantiate(Projectile2, SpawnObject.position, SpawnObject.rotation);
}
if(FireType == 2) {
clone = Instantiate(Projectile3, SpawnObject.position, SpawnObject.rotation);
}
clone.velocity = transform.TransformDirection(Vector3.up * Speed);
}
if(Input.GetButtonDown("Fire2")&&!Input.GetButtonDown("Fire1")) {
var clone2 : Rigidbody;
if(FireType == 0) {
clone = Instantiate(Projectile1, SpawnObject.position, SpawnObject.rotation);
}
if(FireType == 1) {
clone = Instantiate(Projectile2, SpawnObject.position, SpawnObject.rotation);
}
if(FireType == 2) {
clone = Instantiate(Projectile3, SpawnObject.position, SpawnObject.rotation);
}
}
if(Input.GetButtonDown("FireChange")) {
if(FireType <= 1) {
FireType += 1;
} else {
FireType = 0;
}
}
if(Input.GetKeyDown("Destroy")) {
if(FireType==0) {
redCube = GameObject.FindGameObjectsWithTag("RedCube");
for(var i : int=0; i<redCube.Length; i++) {
Destroy(redCube*);*
-
}*
-
}*
-
if(FireType==1) {*
-
blueCube = GameObject.FindGameObjectsWithTag("BlueCube");*
-
for(var j :int=0; i<blueCube.Length; i++) {*
_ Destroy(blueCube*);_
_ }_
_ }_
_ if(FireType==2) {_
_ greenCube = GameObject.FindGameObjectsWithTag(“GreenCube”);_
_ for(var k : int=0; i<greenCube.Length; i++) {_
_ Destroy(greenCube);
}
}
}
}*
What am I doing wrong?_