Equipment system

,

i have a problem that when i swape weapons in the equipment panal it swape weapon correctly but it also keep instantiate the default weapon
idk if its race condtion or what ?


Update weapon is called throw event on the Equipment script which is invoked when we add or remove weapons

For future code pastes, please paste in the text of the script rather than a screenshot.

Let’s add a quick Debug to make sure things are wired correctly

void UpdateWeapon()
{
    Debug.Log("UpdateWeapon()");
    Weapon weapon = equipment.GetItemInSlot(EquipLocation.Weapon) as WeaponSO;
    {
        if(weapon!=null)
        {
              Debug.Log($"Equipment has {weapon.DisplayName} equipped.");
              EquipWeapon(weapon);
        }
        else
        {
              Debug.Log("Equipment does not have a weapon equipped, equipping default weapon.");
              EquipWeapon(_defaultWeaponSO);
        }
}

sry for screenshots
i tried the debugs and its work correctly first it remove the weapon and equipping the default then it equipe the new weapon bt yet the bug still exist

i fixed the problem through modifying the destroyOldWeapon function but i dont know why the bug happen in the first place

public void DestroyOldWeapon(Transform rightHand , Transform leftHand)
        {
            Weapon[] oldWepR = rightHand.GetComponentsInChildren<Weapon>();
            Weapon[] oldWepL = leftHand.GetComponentsInChildren<Weapon>();

            if(oldWepR.Length >= 1 ) 
            {
                foreach (var item in oldWepR)
                {
                    Destroy(item.gameObject);
                }
                
            }
             if(oldWepL.Length >= 1 ) 
            {
                foreach (var item in oldWepL)
                {
                    Destroy(item.gameObject);
                }   
            }          

        }

Something wasn’t getting everything destroyed…
You can also use something like this

foreach(Transform child in rightHand)
{
    Destroy(child.GameObject);
}
foreach(Transform child in leftHand)
{
    Destroy(child.GameObject);
}

or if you really want to get fancy:

void DestroyChildren(Transform transform)
{
     foreach(Transform child in transform)
     {
           Destroy(child.gameObject);
     }
}

public void DestroyOldWeapon(Transform rightHand, Transform leftHand)
{
     DestroyChild(rightHand);
     DestroyChild(leftHand);
}

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

Privacy & Terms