Deactive , Activate objects in Array based on other integer

I read almost all unity forum post on this topic one is about activating multiple at a time , others are in java script (have no clue how to convert ) and well i tought i could ask here well i want my wepons to be activated deactivated and i want to put them into aray because i would have lots of game object references

i have and aray of weapon game obects

    public GameObject [] guns;

and i have my int

public int currentweaponprimary = 0;
public int currentweaponsecondary = 0;

and well since its in and aray i cant use smt like guns.Setactive(false or true) so how can i activate deactive them by those integers

If I got you right you need something like this:

  public void ChangeWeapon(int weaponIndex) // weaponIndex is the index of weapon we need to activate
    {
        foreach (Transform weapon in weaponsHolderObject.transform) // weaponsHolderObject is GameObject which is parent of all weapons
        {
            if (weapon.GetSiblingIndex() == weaponIndex)
                weapon.gameObject.SetActive(true); // if weaponIndex is the index of current child then we activate it
            else weapon.gameObject.SetActive(false);// and deactivate every other weapons

        }
    }

and then you call it like this

 ChangeWeapon(currentweaponprimary);
1 Like

t

thank you i will try it out now :slight_smile:

try it and tell me if it helps. This is my first reply here, I hope it will help you :smile:

somehow iam geting this
Assets/Scripts/Mangers/OtherManagers/weapons.cs(50,17): error CS1501: No overload for method ChangeWeapon' takes 0’ arguments

void Update ()
{
ChangeWeapon (/*i believe iam mising arguments here */);
}

ow dam sorry dint read your last post till the end wher you need to put currenweapontprimary in the () you help me a lot realy thank you and i think this post will help others to