Finding which object is needed and determine how much i have in the inventory

Ok so i’m making a farming game and i’m working on an order system and i’m trying to get the script to look in the inventory to see how much of the specific item i have currently.

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


public class OrderAction : MonoBehaviour
{
    public string item1Name = string.Empty;
    public int item1Amount;
    public Image item1Image;
    public Text item1Text;
    int item1amounthave;
    [Space(10)]
    public string item2Name = string.Empty;
    public int item2Amount;
    public Image item2Image;
    public Text item2Text;
    int item2amounthave;
    [Space(10)]
    public string item3Name = string.Empty;
    public int item3Amount;
    public Image item3Image;
    public Text item3Text;
    int item3amounthave;
    [Space(10)]
    public string item4Name = string.Empty;
    public int item4amount;
    public Image item4Image;
    public Text item4Text;
    int item4amounthave;
    [Space(10)]
    public string item5Name = string.Empty;
    public int item5Amount;
    public Image item5Image;
    public Text item5Text;
    int item5amounthave;
    [Space(10)]
    public string item6Name = string.Empty;
    public int item6Amount;
    public Image item6Image;
    public Text item6Text;
    int item6amounthave;
    [Space(10)]

    public GameObject currentOrderBig;
    private GameObject bigorder;

    Barn barn;
    Silo silo;
    Inventory inventory;
    orderBoard orderBoard;

    // Use this for initialization
    void Start()
    {
        barn = GameObject.FindGameObjectWithTag("Barn").GetComponent<Barn>();
        silo = GameObject.FindGameObjectWithTag("Silo").GetComponent<Silo>();
        inventory = GameObject.FindGameObjectWithTag("Inventory").GetComponent<Inventory>();
        orderBoard = GameObject.FindGameObjectWithTag("OrderBoard").GetComponent<orderBoard>();
    }

    // Update is called once per frame
    void Update()
    {
        item1Text.text = "" + item1amounthave + "/" + "" + item1Amount;
    }

    public void showBigUi()
    {
        bigorder = Instantiate(currentOrderBig, orderBoard.bigOrderPoint.transform.position, orderBoard.bigOrderPoint.transform.rotation) as GameObject;
        gameObject.transform.parent = bigorder.transform;
    }
}

In the inspector i set all the values so for example the first one i would type in Wheat for “item1Name” and something like 3 for “item1Amount” which is how much i need for the order.

 void Update()
    {
        item1amounthave = inventory.
        item1Text.text = "" + item1amounthave + "/" + "" + item1Amount;
    }

what im trying to do is to have the “item1amounthave” (which is how much of the item i have) integer change to how much of the item i have in the inventory script. But i want it to figure out which item it needs to check for using the name i set in the inspector. So if im using wheat then the name of the integer in the inventory would be wheatCount. The only way i can think of to do it is to use an if statement for every item that i have but thats obviously not a good idea. ive tried looking it up but i dont really know how i would word this question in google.

1 Answer

1

Arrays are your friend here. You need to do a bit of restructuring.
First, you should have a class that holds item info. Something like this:

[System.Serializable]
public class ItemInfo {
    public string Name;
    public int Cost;
    public Image UiImage;
    public Text UiText;
    public int Count;
    // a constructor would probably be nice too!
}

Then in your OrderAction class have an array of ItemInfo. Like this:

public ItemInfo[] itemInfos;

Then you iterate through the array of ItemInfo looking for the Name that matches the item you’re looking for and increase or deduct the Count value. Something like this

public ItemInfo GetItemInfo( string name ) {
    for (int i = 0; i < itemInfos.Length; i++) {
        if (itemInfos *== name)*

return itemInfos*;*
}

return null;
}

void SomeOtherMethod() {
ItemInfo myItem = GetItemInfo(“TheItemsName”);
if (myItem != null) {
myItem.Count++;
Debug.LogFormat(“Item named: {0} now has count of : {1}”, myItem.Name, myItem.Count);
}
}