- Home /
weighted inventory system
[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;
public float Weight { get { return weight; } set { weight = value; } } }
public class Inventory : MonoBehaviour
{ [SerializeField] List items; [SerializeField] Transform itemsParent; [SerializeField] ItemSlot[] itemSlots; public Item item; public float currentweight = 0; public float totalweight = 100;
private void RefreshUI()
{
int i = 0;
for(; i < items.Count && i < itemSlots.Length; i++)
{
itemSlots[i].item = items[i];
currentweight += item.weight;
}
for(; i < itemSlots.Length; i++)
{
itemSlots[i].item = null;
}
}
}
I'm trying to add a weight system to my inventory like fallout but it just take information from my one scriptable object rather than the item in the slot. How can I make the script read the item in slots indivisual rather than setting a value for all items.
Answer by xxmariofer · Jan 13, 2019 at 12:08 PM
hello @jonathanbooker2911, first just an advice if you are creating a property Weight for returning weight, mark weight as private since having both accesible when both are the same var it is a bit confusing. you are adding item.weight (not sure if you assigned that var in the editor) just change
currentweight += item.weight;
to
currentweight += itemSlots[i].item.Weight;
hope it helps.
Your answer
Follow this Question
Related Questions
Attacking mulitple targets simultanously 2 Answers
Linking Image to item list 1 Answer
Help with falling enemy Unity 3D 0 Answers
How to return control back to the original script? 1 Answer
Trying to find the highest number than add it to itself. 2 Answers