x


Disable objects that dose not equal Index(array)

Hi.

I have a script that disables weapons depending on the Index of an array. Something like so:

var weapons : GameObject[];
var currentWeapon = 0;

function Update () {
    if(Input.GetButtonDown("YButton")){
    currentWeapon += 1;
    if(currentWeapon == 0){
    weapons[currentWeapon].SetActiveRecursively(true);
        weapons[!=currentWeapon].SetActiveRecursively(false); //HERE
    }
    else if(currentWeapon == 1){
    weapons[currentWeapon].SetActiveRecursively(true);
    }
    else if(currentWeapon == 2){
    weapons[currentWeapon].SetActiveRecursively(true);
    }
}
}

But how can I easily set SetActiveRecursively to objects that dose not equal currentWeapon? Some thing like so:

weapons[!=currentWeapon].SetActiveRecursively(false);

Instead of "hard-code" it the above example would be flexible but it gives errors, though.

I hope you get what I mean.

Thank you.

more ▼

asked Aug 28 '11 at 08:20 PM

Mathias gravatar image

Mathias
268 8 13 18

(comments are locked)
10|3000 characters needed characters left

3 answers: sort newest

You could use a For loop to set the first item in the loop to true and everything else to false.

if(currentWeapon == 0){     
    for (int i; i < weapons.length; i++) {
       if (i == 0) {
         weapons[currentWeapon].SetActiveRecursively(true);
       } else {
         weapons[!=currentWeapon].SetActiveRecursively(false);
       }
    }
}
else if(currentWeapon == 1)
{
    weapons[currentWeapon].SetActiveRecursively(true);
}
else if(currentWeapon == 2)
{
    weapons[currentWeapon].SetActiveRecursively(true);
}

(Sorry. I don't know the JS syntax but you should get the general idea.)

more ▼

answered Aug 28 '11 at 10:14 PM

AaronG gravatar image

AaronG
261 16 18 22

(comments are locked)
10|3000 characters needed characters left

Both of the answers would work - my question is which would execute faster? Or is the difference neglible?

more ▼

answered Oct 13 '11 at 10:33 AM

bkovner gravatar image

bkovner
1 1 2 2

(comments are locked)
10|3000 characters needed characters left
var weapons : GameObject[];
var currentWeapon = 0;

function Update () {
currentWeapon  = Mathf.Clamp (currentWeapon , 0, weapons.length);
    if(Input.GetButtonDown("YButton")){
    currentWeapon += 1;
    }
selectWeapon(currentWeapon );
}

function selectWeapon(index : int){
for (var i=0;i<transform.childCount;i++){
// Activate the selected weapon
    if (i== index)
       transform.GetChild(i).gameObject.SetActiveRecursively(true);
// Deactivate all other weapons
    else
       transform.GetChild(i).gameObject.SetActiveRecursively(false);
    }
}
more ▼

answered Aug 28 '11 at 11:35 PM

unluckyBastard gravatar image

unluckyBastard
136 1 1 4

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1363
x31

asked: Aug 28 '11 at 08:20 PM

Seen: 607 times

Last Updated: Oct 13 '11 at 10:33 AM