1
\$\begingroup\$

I want to make an inventory system for my game, and I found some issues when collecting an item.

What I want is to match the index in the Inventory_UI system exactly to the currentIndex on the collected item, then display it on the Inventory_UI system.

Let's say I pick up an item with currentIndex = 1, so in the Inventory_UI system I want to set index=1 too and so on. This happens with the inventory description too, so the id is the same as the itemIndex picked up.

Here is my ItemManager script, holding the information about the items:

[System.Serializable]
public class theItems
{
    public string Name;
    public GameObject[] allCurrentPrefabs;
    public int numberOfIndex;
    public AudioClip[] allSound;
    public string[] terjemahannya;
    public string[] latinnya;
}

public class _ItemManager : MonoBehaviour
{
    public static _ItemManager instance;

    private void Awake()
    {
        instance = this;
    }

    public List<theItems> items;

    [Header("SurahApa")]
    public int Surah;

    [Header("SoundStuff")]
    public AudioSource aSource;


    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    public void PlaySurah(int surahnya, int ayatnya)
    {
        aSource.clip = items[surahnya].allSurahSound[ayatnya];
        aSource.Play();
    }
}

Next my CollectibleItem, a component attached to each item.

 public static _CollectibleAyat instance;

    private void Awake()
    {
        instance = this;
    }

    public enum InteractionType
    {
        none, pickUp, Examine
    }

    public InteractionType interactType;

    public int currentIndex;

    public string terjemahannya;
    public string latinnya;
    public GameObject allCurrentPrefabs;

    public UnityEvent customevent;

    public void Reset()
    {
        GetComponent<Collider2D>().isTrigger = true;
    }

    public void interact()
    {
        switch (interactType)
        {
            //case InteractionType.none:
            //    Debug.Log("None");
            //    break;
            case InteractionType.Examine:

                _InteractSystem.instance.Examine(this);

                Debug.Log("Examine");

                break;
            case InteractionType.pickUp:
                // Add the object to the pickUpItems List
                // Delete the Object

                _InventorySystem.instance.PickUpItem(gameObject);
                gameObject.SetActive(false);

                Debug.Log("PickingUp");
                break;
            default:

                Debug.Log("Null Item");
                break;
        }

        //Invoke (Call) the custom Event(s)
        customevent.Invoke();
    }

And my InventorySystem, which handles when items are picked up.

public static _InventorySystem instance;

    private void Awake()
    {
        instance = this;
    }

    [Header("GeneralFields")]
    public List<GameObject> items = new List<GameObject>();
    public bool isOpen;
    public int ayah;

    [Header("UI Items Section")]
    public GameObject UI_InventoryWindow;
    public Image[] item_Images;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.I))
        {
            ToggleInventory();
        }
    }

    public void ToggleInventory()
    {
        isOpen = !isOpen;
        UI_InventoryWindow.SetActive(isOpen);
    }

    public void PickUpItem(GameObject item)
    {
        items.Add(item);
        Update_UI();
    }

    public void Update_UI()
    {
        HideAll();

        for (int i = 0; i < items.Count; i++)
        {
            item_Images[i].sprite = items[i].GetComponent<SpriteRenderer>().sprite;
            item_Images[i].gameObject.SetActive(true);

        }

    }

    public void HideAll()
    {
        foreach (var i in item_Images)
        {
            i.gameObject.SetActive(false);
        }
    }

    //SampeSini
    public void ShowDescription(int id)
    {


    }

    public void HideDescription()
    {

    }

And lastly my InteractSystem which handles interacting with and storing items in the collectibleItem system.

 public static _InteractSystem instance;

    private void Awake()
    {
        instance = this;
    }

    public Transform detectionPoint;

    public const float detectionRadius = .2f;

    public LayerMask detectionLayer;

    public GameObject detectedObject;

    public GameObject examinePanel;

    private void Update()
    {
        if (DetectedObject())
        {
            if (PickUpInput())
            {
                detectedObject.GetComponent<_CollectibleAyat>().interactType = _CollectibleAyat.InteractionType.pickUp;
                detectedObject.GetComponent<_CollectibleAyat>().interact();
                _UIManager.instance.BackToGameFromExamine();
            }

            if (ExamineInput())
            {
                detectedObject.GetComponent<_CollectibleAyat>().interactType = _CollectibleAyat.InteractionType.Examine;
                detectedObject.GetComponent<_CollectibleAyat>().interact();
            }
        }
    }

    bool PickUpInput()
    {
        return CrossPlatformInputManager.GetButtonDown("Fire1");
    }

    bool ExamineInput()
    {
        return CrossPlatformInputManager.GetButtonDown("Fire2");
    }

    bool DetectedObject()
    {
        Collider2D obj = Physics2D.OverlapCircle(detectionPoint.position, detectionRadius, detectionLayer);
        if (obj == null)
        {
            detectedObject = null;
            return false;
        }
        else
        {
            detectedObject = obj.gameObject;
            return true;
        }
    }

    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.green;
        Gizmos.DrawSphere(detectionPoint.position, detectionRadius);
    }

    public void Examine(_CollectibleAyat items)
    {
        examinePanel.SetActive(true);

        items.terjemahannya = _QuranManager.instance.quran[_QuranManager.instance.Surah].terjemahannya[items.currentAyah].ToString();
        items.latinnya = _QuranManager.instance.quran[_QuranManager.instance.Surah].latinnya[items.currentAyah].ToString();
        items.allCurrentSurahPrefabs = _QuranManager.instance.quran[_QuranManager.instance.Surah].allCurrentSurahPrefabs[items.currentAyah];

        var go = Instantiate(items.allCurrentSurahPrefabs, _UIManager.instance.instantiatePosition.position,Quaternion.identity);
        go.transform.SetParent(_UIManager.instance.instantiatePosition.transform);

        _UIManager.instance.terjemahanText.text = items.terjemahannya;
        _UIManager.instance.latinText.text = items.latinnya;
    }

Here is what it looks like when running in the editor:

Note: in ShowDescription(int id), I want to show item information matching the item I picked up.

\$\endgroup\$
1
  • \$\begingroup\$ The non-English variable names make it a bit challenging for us to understand what you want to happen here. It looks like your theItems class represents a chapter, consisting of a sequence of verses. Each collectible item references a particular verse index, but doesn't know what chapter to read from. When you examine an item, you look up the current chapter from the Inventory System to then fetch the variables related to verse "x" in the current chapter, is that right? \$\endgroup\$
    – DMGregory
    Mar 6 at 14:39
0
\$\begingroup\$

Presumably you want something like this:

[SerializeField] private List<CollectibleItem> items = new List<CollectibleItem>();
private int index;
// ...
public void PickUpItem(CollectibleItem item) {
    items.Add(item);
    this.index = item.currentIndex; //Why do you need this exactly?
    Update_UI();
}

Your InventorySystem does not have a field named index so I'm not sure exactly why you want to read the index. However, I am pretty sure that whatever you're trying to do, you're trying to do it the wrong way.

I can't think of any reason why a collectible item would need a public currentIndex field that the inventory system would need to read. If you're planning to use that index to retrieve some data (such as a sprite) associated with the item, you should instead read the data directly from the item.

If you are planning to use the "index" to sort the items list, don't call it an "index". Call it sortOrder or something like that.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.