0

The problem is I have a class for a potato, and when i use it via inventory, it should take a use off the potato, and only that potato. Instead, its taking it off every potato in the game, is there a way to make it where all the potatos have their own instance of the code?

public class Food : Item {
    public float healthHealedOnUse;
    public int uses;
}//name ect is in the Item base class

Code I am using for Inventory:

    public void UseItem(){
    if (item != null) {
        if (item is Food) {
            Debug.Log ("using "+item.name);
            PHH.Heal (((Food)item).healthHealedOnUse);
            ((Food)item).uses--;
            if(((Food)item).uses < 1){
                ClearSlot();
            }
        } else {
            item.Use ();
        }

It is effecting all potatos, not just the one I click on.

Adding to Inventory

public List<Item> items = new List<Item>();
public bool add(Item item){
    if (items.Count >= space) {
        Debug.Log ("Inventory Full");
        return false;
    } else {
        items.Add (item);
        onItemChangedCallback.Invoke ();
        return true;
    }
}

Player picking up item script

void PickUp(){
    Debug.Log ("Picking up " + item.name+"!");
    bool wasPickedUp = Inventory.instance.add (item);
    //Debug.Log(wasPickedUp);
    if (wasPickedUp == true) {
        Destroy (gameObject);
    }

Code adding item into a inventory slot

public void AddItem(Item newItem){
    item = newItem;
    icon.sprite = item.icon;
    icon.enabled = true;
    removeButton.interactable = true;
}

How i am displaying items in inventory slots

Inventory inventory;
public GameObject Inventoryui;
public Transform itemsParent;
InventorySlot[] slots;
void Start () {
    Inventoryui.SetActive (false);
    inventory = Inventory.instance;
    inventory.onItemChangedCallback += UpdateUI;
    slots = itemsParent.GetComponentsInChildren<InventorySlot> ();
}

public void ToggleInventory(){

    Inventoryui.SetActive (!Inventoryui.activeSelf);
}
void UpdateUI(){
    Debug.Log ("Updating UI");
    for (int i = 0; i < slots.Length; i++) {
        if (i < inventory.items.Count) {
            slots [i].AddItem (inventory.items [i]);
        } else {
            slots [i].ClearSlot ();
        }
    }
}
7
  • We need to see more code. How are you adding items to the inventory? Commented Jan 8, 2018 at 3:10
  • Also, you should just make your Food class's Use() method heal the player. That's the point of subclassing: Item#Use is marked virtual and then Food#Use is marked override and you can just make UseItem() call item.Use(), done. Commented Jan 8, 2018 at 3:12
  • there you go, i added the additem in there Commented Jan 8, 2018 at 4:24
  • Regarding the Food's use class, i thought this would be better then having to find the player component, then get the player stats handler off of player. Commented Jan 8, 2018 at 4:25
  • I turned that into an answer on your other question. Regarding this...I now need to know where you're calling add from: that is, I need to see how you get multiple potatoes into your inventory. Odds are you're doing something like var POTATO = new Food(); /*...*/ Player.inventory.add(POTATO); causing all of your inventory items to be references to the same object. Commented Jan 8, 2018 at 4:28

1 Answer 1

1

...which takes it a scriptible item

There's your "every reference is the same" problem.

You need to either:

  1. clone the scriptable item when it's picked up and add the clone to the player's inventory
  2. treat the scriptable item as a singleton that describes what an item is and create an "ItemStack" class the way Minecraft does (there is only one Potato object, but all stacks of potatoes are an instance of ItemStack which contains a reference to the idealized potato instance).
Sign up to request clarification or add additional context in comments.

2 Comments

where would I clone the scriptable? within the prefab?
@BrandonG Anytime between when you instantiate the prefab and when it gets added to the player's inventory. Personally I'd do it as part of the add method you've already got.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.