system
1
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.
AaronG
2
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.)
system
3
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);
}
}
system
4
Both of the answers would work - my question is which would execute faster? Or is the difference neglible?