Example #1
0
    private void LoadUnitWeapons(Transform unitClone, string unitName) // CALLED WHEN SPAWNING UNITS
    {
        UnitWeaponLoadout[] weaponLoadouts = null;
        UnitWeaponLoadout   curLoadout     = null;
        int curUnit;

        if (currentUnit.teamLayer == 9)
        {
            weaponLoadouts = LoadoutSwitcharoo.Instance.GetTeam2UnitLoadouts;
        }
        else if (currentUnit.teamLayer == 10)
        {
            weaponLoadouts = LoadoutSwitcharoo.Instance.GetTeam1UnitLoadouts;
        }

        foreach (UnitWeaponLoadout loadout in weaponLoadouts)
        {
            string str1 = loadout.name.ToLower().Replace(" ", string.Empty);
            string str2 = unitName.ToLower().Replace(" ", string.Empty);

            if (str1 == str2)
            {
                curLoadout = loadout;
                //Debug.Log(str1 + " == " + str2);
                break;
            }
        }

        UnitBase unit = unitClone.GetComponent <UnitBase>();

        curUnit = LoadoutSwitcharoo.Instance.GetUnit;
        for (int i = 0; i < unit.weapons.Length; i++)
        {
            if (curLoadout)
            {
                WeaponBase weaponClone = Instantiate(curLoadout.weapons[i]); // here
                weaponClone.transform.parent        = unitClone;
                weaponClone.transform.localPosition = Vector3.zero;

                unit.weapons[i]       = weaponClone;
                unit.weapons[i].owner = unitClone;
            }
            else
            {
                Debug.Log(curLoadout + " | " + currentUnit.unitPrefab);
                Debug.Log("Error..");
                Debug.Break();

                return;
            }
        }

        unit.InitializeWeapons();
    }