Stats equipable item fails to load prefab

Hello,

I just finished the Stats equipable item part, created my own items and the problem is, that the game allows me to equip the item and all, but it does not give me the weapon itself (if that makes sense).

In other words, I pick up a Stats equipable item, find it in my inventory, put it to the weapon slot, but my character’s hands are still empty and even the inspector says that I’m still unarmed.

Other non-visible gear like head slots, leg slots, rings etc. work fine, because they give only passive bonus to health for example. But this weapon one needs to be seen and used which is currently not.

What am I missing?

Thank you for any input.

image

I’m not seeing an Equipped Prefab in the inspector you posted.
Post up a copy of your WeaponConfig.cs (format it with the </> button) and we’ll take a look.

Hello Brian,

here it is. Just note that the previous image is StatsEquipableItem, not a WeaponConfig itself (even though it should inherit from it)

namespace Game.Combat
{
    [CreateAssetMenu(fileName = "Weapon", menuName = "Items/Weapon", order = 0)]
    public class WeaponConfig : EquipableItem, IModifierProvider
    {
        [SerializeField] private Weapon weaponPrefab = null;
        [SerializeField] private AnimatorOverrideController weaponOverrideAnimatorController = null;

        [SerializeField] private float weaponRange = 2f;
        [SerializeField] private float weaponDamage = 10f;
        [SerializeField] private float damageBonus = 0;
        [SerializeField] private float percentageBonus = 0;

        [SerializeField] private bool isRightHanded = true;

        [SerializeField] private Missile missile = null;

        private const string weaponName = "Weapon";

        public Weapon Spawn(Transform rightHand, Transform leftHand, Animator animator)
        {
            DestroyPreviousWeapon(rightHand, leftHand);

            Weapon weapon = null;

            if (weaponPrefab != null)
            {
                Transform handTransform = GetHandTransform(rightHand, leftHand);
                weapon = Instantiate(weaponPrefab, handTransform);
                weapon.gameObject.name = weaponName;
            }

            var overrideController = animator.runtimeAnimatorController as AnimatorOverrideController;
            if (weaponOverrideAnimatorController != null)
            {
                animator.runtimeAnimatorController = weaponOverrideAnimatorController;
            }
            else if (overrideController != null)
            {
                animator.runtimeAnimatorController = overrideController.runtimeAnimatorController;
            }

            return weapon;
        }

        private void DestroyPreviousWeapon(Transform rightHand, Transform leftHand)
        {
            Transform previousWeapon = rightHand.Find(weaponName);

            if (previousWeapon == null)
            {
                previousWeapon = leftHand.Find(weaponName);
            }

            if (previousWeapon == null) return;

            previousWeapon.name = "Weapon_DESTROYED";
            Destroy(previousWeapon.gameObject);
        }

        private Transform GetHandTransform(Transform rightHand, Transform leftHand)
        {
            Transform handTransform;

            if (isRightHanded)
            {
                handTransform = rightHand;
            }
            else
            {
                handTransform = leftHand;
            }

            return handTransform;
        }

        public float GetDamageBonus()
        {
            return damageBonus;
        }

        public float GetPercentageBonus()
        {
            return percentageBonus;
        }

        public float GetWeaponRange()
        {
            return weaponRange;
        }

        public float GetWeaponDamage()
        {
            return weaponDamage;
        }

        public bool HasMissile()
        {
            return missile != null;
        }

        public void LaunchMissile(Transform rightHand, Transform leftHand, Health target, GameObject instigator, float calculatedDamage)
        {
            Missile missileInstance = Instantiate(missile, GetHandTransform(rightHand, leftHand).position, Quaternion.identity);
            missileInstance.SetTarget(target, instigator, calculatedDamage);
        }

        public IEnumerable<float> GetAdditiveModifiers(Stat stat)
        {
            if (stat == Stat.AttackDamage)
            {
                yield return weaponDamage;
            }
        }

        public IEnumerable<float> GetPercentageModifiers(Stat stat)
        {
            if (stat == Stat.AttackDamage)
            {
                yield return percentageBonus;
            }
        }
    }
}
namespace Game.Inventories
{
    [CreateAssetMenu(menuName = ("Inventory/Stats Equipable Item"))]
    public class StatsEquipableItem : EquipableItem, IModifierProvider
    {
        [SerializeField]
        private Modifier[] additiveModifiers;

        [SerializeField]
        private Modifier[] percentageModifiers;

        [System.Serializable]
        private struct Modifier
        {
            public Stat stat;
            public float value;
        }

        public IEnumerable<float> GetAdditiveModifiers(Stat stat)
        {
            foreach (var modifier in additiveModifiers)
            {
                if (modifier.stat == stat)
                {
                    yield return modifier.value;
                }
            }
        }

        public IEnumerable<float> GetPercentageModifiers(Stat stat)
        {
            foreach (var modifier in percentageModifiers)
            {
                if (modifier.stat == stat)
                {
                    yield return modifier.value;
                }
            }
        }
    }
}

It should be identicatl with the code that is available in github repo.

Thank you for reaching out so fast.

I think I see the issue… the bow should be a WeaponConfig, not a StatsEquippableItem. The inspector you have posted is a StatsEquippableItem, which Fighter will not recognize as a WeaponConfig.

Yes, that was indeed the issue. The root of the problem was that it did not add any damage bonus. That’s why I started digging in there in the first place.

I don’t know if I just missed a line there, but the GetAdditive/Percentage modifiers just did not work in my case, so I workarounded it by adding damageBonus in WeaponConfig like this:

public float GetWeaponDamage()
        {
            return weaponDamage + damageBonus;
        }

…and it works flawlessly in my use case. I spent too much time thinking how to make it work with Stats item instead doing this right away.

Thanks for your help tho :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms