SetLayerWeight using Arrays

How i can use arrays here:

        if (weapons == Weapons.Pistol)
        {
            anim.SetLayerWeight(weaponsList[0].weaponId, 1);
            anim.SetLayerWeight(weaponsList[1].weaponId, 0);

            weaponsList[0].weaponPrefab.SetActive(true);
            weaponsList[1].weaponPrefab.SetActive(false);
        }

Full code:

using UnityEngine;
using System.Collections;

[System.Serializable]
public class WeaponsList
{
    [Header("-Weapon Name-")]
    public string weaponName;

    [Header("-Weapon Prefab-")]
    public GameObject weaponPrefab;

    [Header("-Animator Layer Index-")]
    public int weaponId;
}




public class PlayerWeapons : MonoBehaviour
{
    Animator anim;

    public enum Weapons { Pistol, M4 };

    public Weapons weapons;

    public WeaponsList[] weaponsList;

    float timer;

    void Start()
    {
        anim = GetComponent<Animator>();
    }

    void Update()
    {
        if (Input.GetButton("Fire1"))
        {
            anim.SetBool("IsShooting", true);
        }
        else
        {
            anim.SetBool("IsShooting", false);
        }


        if (weapons == Weapons.Pistol)
        {
            anim.SetLayerWeight(weaponsList[0].weaponId, 1);
            anim.SetLayerWeight(weaponsList[1].weaponId, 0);

            weaponsList[0].weaponPrefab.SetActive(true);
            weaponsList[1].weaponPrefab.SetActive(false);
        }

        if (weapons == Weapons.M4)
        {
            anim.SetLayerWeight(weaponsList[0].weaponId, 0);
            anim.SetLayerWeight(weaponsList[1].weaponId, 1);

            weaponsList[0].weaponPrefab.SetActive(false);
            weaponsList[1].weaponPrefab.SetActive(true);
        }
    }
}

Well, you ARE technically using arrays, but I assume you’re asking how to use them efficiently (so that they’ll adapt to different sizes). You need a for loop:

int activeWeapon = 1; // I'm guessing this is what you want here? One of these to be active, while all others are inactive?
for (int w=0; w<weaponsList.Length;w++) {
float weight = 0f;
if (w == activeWeapon) weight = 1f;
anim.SetLayerWeight(weaponsList[w].weaponId, weight);
weaponsList[w].weaponPrefab.SetActive(w == activeWeapon);
}
1 Like

is not working so, good, i mean he change the Weight for the all layers

EDIT: i change the weapon id to start from 0 (i mean pistol id is 0 and m4 is 1), and when i have weapon id 1 si the layer from id 0.

if i put the id to start from 1, example Pistol id 1 and M4 id is 2, the both layers have Weight to 1.

EDIT 2: To work i made this, but i when i add a new weapon, i don’t want to add every time new lines in code:

using UnityEngine;
using System.Collections;

[System.Serializable]
public class WeaponsList
{
    [Header("-Weapon Name-")]
    public string weaponName;

    [Header("-Weapon Prefab-")]
    public GameObject weaponPrefab;
    public int weaponId;

    [Header("-Weapon Layer Index-")]
    public int weaponLayerIndex;
    [Tooltip("Weapon Layer Index is: Weapon Id + 1")]
    public bool idAdd1Layer = true;

    public void checkIdAdd()
    {
        if(idAdd1Layer)
        {
            weaponLayerIndex = weaponId + 1;
        }
    }
}


public class PlayerWeapons : MonoBehaviour
{
    public static PlayerWeapons instance;

    Animator anim;

    public enum Weapons { Pistol, M4 };


    [Header("-Weapons Settings-")]
    public Weapons weapons;
    public WeaponsList[] weaponsList;

    [Header("-Current Weapon-")]
    public int activeWeaponId = 0;

    [Header("-Weapon Animations-")]
    public string shootAnimBoolName = "IsShooting";

    //public float weight = 0f;

    void Start()
    {
        anim = GetComponent<Animator>();

        for (int i = 0; i < weaponsList.Length; i++)
        {
            weaponsList[i].checkIdAdd();
        }
    }

    void Update()
    {

        for (int i = 0; i < weaponsList.Length; i++)
        {
            //if (i == activeWeaponLayer) weight = 1f;
            //anim.SetLayerWeight(weaponsList[i].weaponId, weight);

            weaponsList[i].weaponPrefab.SetActive(i == activeWeaponId);

            weaponsList[i].checkIdAdd();
        }

        if (weapons == Weapons.Pistol)
        {
            activeWeaponId = 0;
        }

        if (weapons == Weapons.M4)
        {
            activeWeaponId = 1;
        }






        if(activeWeaponId == 0)
        {
            anim.SetLayerWeight(weaponsList[0].weaponLayerIndex, 1);
            anim.SetLayerWeight(weaponsList[1].weaponLayerIndex, 0);
        }

        if (activeWeaponId == 1)
        {
            anim.SetLayerWeight(weaponsList[0].weaponLayerIndex, 0);
            anim.SetLayerWeight(weaponsList[1].weaponLayerIndex, 1);
        }


        if (Input.GetButton("Fire1"))
        {
            anim.SetBool(shootAnimBoolName, true);
        }
        else
        {
            anim.SetBool(shootAnimBoolName, false);
        }
    }
}