Adding One weapon at a time

Zip up your project and upload it to https://gdev.tv/projectupload and I’ll take a closer look.
Be sure to remove the Library folder to conserve space. It may take some time to go through it, as I need to leave for the evening.

Ok, this was amazingly close to ready!
Believe it or not, the weapon was enabled, and if you would have selected it, it would have worked… unfortunately, I forgot that the sprite is inactive, and forgot to have you activate the sprite when the WeaponConfig was added to the slot.

Here’s the fix in InventorySlot.cs

  public void AssingWeaponInfor(WeaponInfo weaponInfo) // NEW for active weapon.
  {
    Debug.Log($"{gameObject.name} is assigning a weapon.");
    this.weaponInfo = weaponInfo;
    transform.GetChild(1).GetComponent<Image>().sprite = weaponInfo.sprite;
    transform.GetChild(1).gameObject.SetActive(true);
    Debug.Log(weaponInfo.weaponRange);
  }

I’m not seeing it being added to the end of the inventory.

did you add the line

transform.GetChild(1).gameObject.SetActive(true);

That’s literally all I did to make it appear.


Yes I did. In the InventorySlot script.

  public void AssingWeaponInfor(WeaponInfo weaponInfo) // NEW for active weapon.
  {
    Debug.Log($"{gameObject.name} is assigning a weapon.");
    this.weaponInfo = weaponInfo;
    transform.GetChild(1).GetComponent<Image>().sprite = weaponInfo.sprite;
    Debug.Log(weaponInfo.sprite);
    transform.GetChild(1).gameObject.SetActive(true);// <--here 
    Debug.Log(weaponInfo.weaponRange);
  }

what does your ActiveInventory look like?
I have:

    public  InventorySlot GetEmptyInventorySlot() // NEW for active weapon.
        {
            foreach (Transform child in transform)
            {
                if (child.TryGetComponent(out InventorySlot inventorySlot)) return inventorySlot;
                Debug.Log("Another Test");
            }

            return null;
        }

and the Debug Log does not show up for me.

does your InventorySlot GetEmptyInventorySlot() look like this?

Ah, I see it now. I did make one other change when I was putting in the Debugs to figure out what was going on, and then when I figured out that the sprite wasn’t getting enabled, I forgot about that detail.

    public  InventorySlot GetEmptyInventorySlot() // NEW for active weapon.
        {
            foreach (Transform child in transform)
            {
                if (child.TryGetComponent(out InventorySlot inventorySlot) )
                {
                    if (inventorySlot.GetWeaponInfo()==null)
                    {
                        Debug.Log($"Returning {inventorySlot.gameObject.name}");
                        return inventorySlot;
                    }
                }
                else
                {
                    Debug.Log($"slot {inventorySlot.gameObject.name} has a {inventorySlot.GetWeaponInfo()} equipped.");
                }
                
            }

            return null;
        }

With your code, the GetEmptyInventorySlot always returns the 0th slot, so the sword is getting overwritte by the… sword…

That did it!! Thank you so much for your help. So I can access any script that is not on the game object?

I’m not sure the context there…
For static functions in a script, you can always access a public static method, property, or variable with

ClassName.MethodName()

To access an instance of a script (component), you need a reference to either the GameObject or transform of the instance, or a reference to another instance of the script.
You can always access components on the same GameObject the current script is running on.

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

Privacy & Terms