Weighted inventory Issues

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Inventory : MonoBehaviour
{
    [SerializeField] List<Item> items;
    [SerializeField] Transform itemsParent;
    [SerializeField] ItemSlot[] itemSlots;
    public Item item;
    public float currentweight = 0;
    public float totalweight = 100;
 

    private void Start()
    {

    }

    private void OnValidate()
    {
        if (itemsParent != null)
            itemSlots = itemsParent.GetComponentsInChildren<ItemSlot>();

        RefreshUI();
    }

    private void RefreshUI()
    {
        int i = 0;
        for(; i < items.Count && i < itemSlots.Length; i++)
        {
            itemSlots.item = items;
            currentweight += item.weight;
        }

        for(; i < itemSlots.Length; i++)
        {
            itemSlots.item = null;
        }
    }

    public bool AddItem(Item item)
    {
        if(IsFull())
        return false;

        currentweight += item.weight;

        items.Add(item);
        RefreshUI();
        return true;
    }

    public bool RemoveItem(Item item)
    {
        if (items.Remove(item))
        {
            currentweight -= item.weight;

            RefreshUI();
            return true;
        }
        return false;
    }

    public bool IsFull()
    {
        return items.Count >= itemSlots.Length;
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum ItemQuality
{
    Trash,
    UnCommon,
    Common,
    Rare,
    Epic,
    OneOfAkind,
}

public enum ItemType
{
    Consumable,
    Resource,
    Tool,
    Armor,
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "Item")]
public class Item : ScriptableObject
{
    [Header("ItemStats")]
    //name
    public string itemName;
    //icon
    public Sprite Icon;
    //item description
    public string itemDescription;
    //Id
    public int itemID;
    //Weight
    public float weight;
    //stack
    public int amount;
    //maxstacks
    public int maxstacksize;

    [Header("ItemAppereance")]
    //Item Appreance
    public GameObject gameObject;

    [Header("Price")]
    //price
    public float price;
    //sale price
    public float sellprice;

    [Header("Experience")]
    //xp
    public int expierence;
    //maxxp
    public int maxexperience;
  
    [Header("Level")]
    //curlvl
    public int currentlevel;
    //maxlvl
    public int maxlevel;
    //xpneeded
    public int xpneededtolevel;

    public float Weight
    {
        get { return weight; }
        set { weight = value; }
    }
}

Trying to add my items weight to the current weight on the inventory script I wrote. The way it’s currently setup establishes a value and just set every time to that regardless of indivisual variable. I want it to read the items variable than addd the weight of that item. Not just assume every item has a weight of 5. I’m kind of at a loss on how.

No. Nononono. No.

1 Like

Do I need to remake the thread? I added tags.

1 Like

What up with this function?

    private void RefreshUI()
    {
        int i = 0;
        for(; i < items.Count && i < itemSlots.Length; i++)
        {
            itemSlots.item = items;
            currentweight += item.weight;
        }
        for(; i < itemSlots.Length; i++)
        {
            itemSlots.item = null;
        }
    }

in the first loop you take no account of the current itemSlot, you just do

            itemSlots.item = items;
            currentweight += item.weight;

as many times as there are itemSlots, the second loop won’t ever run because ‘i’ will keep it’s value, why are you declaring it outside the loop?

when you say “assume every item has a weight of 5”, where are you assuming it exactly?
you are “correctly” adding whatever weight an item has to the variable ‘currentWeight’.