Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to find if item is in inventory using these scripts?

Discussion in 'Scripting' started by iFailedPreK, Dec 27, 2019.

  1. iFailedPreK

    iFailedPreK

    Joined:
    Dec 2, 2019
    Posts:
    8
    So I followed some YouTube videos to create an inventory system and I'm currently tweaking them to fit my needs. One need of mine is to find out if a key is in my inventory to be able to open a door. I already know the function I need to check if the trigger and key are pressed, but I also need to know if I have the key. Could someone help me? Thank you for your time.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6. using CodeMonkey.Utils;
    7.  
    8. public class UI_Inventory : MonoBehaviour
    9. {
    10.     private Inventory inventory;
    11.     private Transform itemSlotContainer;
    12.     private Transform itemSlotTemplate;
    13.     private PlayerUpDown player;
    14.  
    15.     public void Awake()
    16.     {
    17.         itemSlotContainer = transform.Find("itemSlotContainer");
    18.         itemSlotTemplate = itemSlotContainer.Find("itemSlotTemplate");
    19.     }
    20.  
    21.     public void SetPlayer(PlayerUpDown player)
    22.     {
    23.         this.player = player;
    24.     }
    25.  
    26.     public void SetInventory(Inventory inventory)
    27.     {
    28.         this.inventory = inventory;
    29.  
    30.         inventory.OnItemListChanged += Inventory_OnItemListChanged;
    31.         RefreshInventoryItems();
    32.     }
    33.  
    34.     private void Inventory_OnItemListChanged(object sender, System.EventArgs e)
    35.     {
    36.         RefreshInventoryItems();
    37.     }
    38.  
    39.     private void RefreshInventoryItems()
    40.     {
    41.         foreach (Transform child in itemSlotContainer)
    42.         {
    43.             if (child == itemSlotTemplate) continue;
    44.             Destroy(child.gameObject);
    45.         }
    46.  
    47.         int x = 0;
    48.         int y = 0;
    49.         float itemSlotCellSize = 100f;
    50.         foreach (Item item in inventory.GetItemList())
    51.         {
    52.             RectTransform itemSlotRectTransform = Instantiate(itemSlotTemplate, itemSlotContainer).GetComponent<RectTransform>();
    53.             itemSlotRectTransform.gameObject.SetActive(true);
    54.  
    55.             itemSlotRectTransform.GetComponent<Button_UI>().ClickFunc = () =>
    56.             {
    57.  
    58.             };
    59.             itemSlotRectTransform.GetComponent<Button_UI>().MouseRightClickFunc = () =>
    60.             {
    61.                 // Drop item
    62.                 inventory.RemoveItem(item);
    63.                 ItemWorld.DropItem(player.GetPosition(), item);
    64.             };
    65.  
    66.             itemSlotRectTransform.anchoredPosition = new Vector2(x * itemSlotCellSize, y * itemSlotCellSize + 100);
    67.             Image image = itemSlotRectTransform.Find("image").GetComponent<Image>();
    68.             image.sprite = item.GetSprite();
    69.  
    70.             x++;
    71.             if (x > 4)
    72.             {
    73.                 x = 0;
    74.                 y++;
    75.             }
    76.         }
    77.     }
    78. }
    79.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5.  
    6. [Serializable]
    7. public class Item
    8. {
    9.     public enum ItemType
    10.     {
    11.         Red,
    12.         Blue,
    13.         Key,
    14.     }
    15.  
    16.     public ItemType itemType;
    17.     public int amount;
    18.  
    19.     public Sprite GetSprite()
    20.     {
    21.         switch (itemType)
    22.         {
    23.             default:
    24.             case ItemType.Red: return ItemAssets.Instance.redItem;
    25.             case ItemType.Blue: return ItemAssets.Instance.blueItem;
    26.             case ItemType.Key: return ItemAssets.Instance.keyItem;
    27.         }
    28.     }
    29.  
    30.     public bool IsStackable()
    31.     {
    32.         switch (itemType)
    33.         {
    34.             default:
    35.             case ItemType.Red:
    36.             case ItemType.Blue:
    37.                 return true;
    38.             case ItemType Key:
    39.                 return false;
    40.         }
    41.     }
    42. }
    43.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5.  
    6. public class Inventory
    7. {
    8.     public event EventHandler OnItemListChanged;
    9.  
    10.     private List<Item> itemList;
    11.  
    12.     public Inventory()
    13.     {
    14.         itemList = new List<Item>();
    15.     }
    16.  
    17.     public void AddItem(Item item)
    18.     {
    19.         if (item.IsStackable())
    20.         {
    21.             bool itemAlreadyInInventory = false;
    22.             foreach (Item inventoryItem in itemList)
    23.             {
    24.                 if (inventoryItem.itemType == item.itemType)
    25.                 {
    26.                     inventoryItem.amount += item.amount;
    27.                     itemAlreadyInInventory = true;
    28.                 }
    29.             }
    30.             if (!itemAlreadyInInventory)
    31.             {
    32.                 itemList.Add(item);
    33.             }
    34.         }
    35.         else
    36.         {
    37.             itemList.Add(item);
    38.         }
    39.         OnItemListChanged?.Invoke(this, EventArgs.Empty);
    40.     }
    41.  
    42.     public List<Item> GetItemList()
    43.     {
    44.         return itemList;
    45.     }
    46.  
    47.     public void RemoveItem(Item item)
    48.     {
    49.         if (item.IsStackable())
    50.         {
    51.             Item itemInInventory = null;
    52.             foreach (Item inventoryItem in itemList)
    53.             {
    54.                 if (inventoryItem.itemType == item.itemType)
    55.                 {
    56.                     inventoryItem.amount -= item.amount;
    57.                     itemInInventory = inventoryItem;
    58.                 }
    59.             }
    60.             if (itemInInventory != null && itemInInventory.amount <= 0)
    61.             {
    62.                 itemList.Remove(itemInInventory);
    63.             }
    64.         }
    65.         else
    66.         {
    67.             itemList.Remove(item);
    68.         }
    69.         OnItemListChanged?.Invoke(this, EventArgs.Empty);
    70.     }
    71. }
    72.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class PlayerUpDown : MonoBehaviour
    7. {
    8.     public float moveSpeed = 5f;
    9.  
    10.     public Rigidbody2D rb;
    11.     public Animator animator;
    12.  
    13.     Vector2 movement;
    14.  
    15.     private Inventory inventory;
    16.  
    17.     [SerializeField] private UI_Inventory uiInventory;
    18.  
    19.     private void Update()
    20.     {
    21.         movement.x = Input.GetAxisRaw("Horizontal");
    22.         movement.y = Input.GetAxisRaw("Vertical");
    23.  
    24.         animator.SetFloat("Horizontal", movement.x);
    25.         animator.SetFloat("Vertical", movement.y);
    26.         animator.SetFloat("Speed", movement.sqrMagnitude);
    27.     }
    28.  
    29.     private void FixedUpdate()
    30.     {
    31.         rb.MovePosition(rb.position + movement * moveSpeed * Time.deltaTime);
    32.     }
    33.  
    34.     private void Awake()
    35.     {
    36.         {
    37.             inventory = new Inventory();
    38.             uiInventory.SetInventory(inventory);
    39.  
    40.             uiInventory.SetPlayer(this);
    41.         }
    42.     }
    43.  
    44.     private void OnTriggerStay2D(Collider2D collider)
    45.     {
    46.         Debug.Log("This is called every frame.");
    47.         ItemWorld itemWorld = collider.GetComponent<ItemWorld>();
    48.         if (itemWorld != null && InputKeyPressed())
    49.         {
    50.             inventory.AddItem(itemWorld.GetItem());
    51.             itemWorld.DestroySelf();
    52.         }
    53.         else if (collider.CompareTag("Door") && InputKeyPressed())
    54.         {
    55.             Debug.Log("Door is unlocked.");
    56.             SceneManager.LoadScene(1);
    57.         }
    58.     }
    59.  
    60.     private bool InputKeyPressed()
    61.     {
    62.         return Input.GetKeyDown(KeyCode.E);
    63.     }
    64.  
    65.     public Vector3 GetPosition()
    66.     {
    67.         return transform.position;
    68.     }
    69. }
    70.  
     
  2. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
    Code (CSharp):
    1. //In your OnCollision Door detect
    2.  
    3. foreach (Item item in inventory.GetItemList())
    4. {
    5.     if (item.itemType == ItemType.Key)
    6.     {
    7.         //Open door
    8.         //Remove key
    9.         //Add effect .. etc...
    10.     }
    11. }

    If you have different keys then you have to modify the item class and add a more complex key structure
    Maybe you want to open a blue door with a blue key (or something like that)
     
  3. iFailedPreK

    iFailedPreK

    Joined:
    Dec 2, 2019
    Posts:
    8
    AND VWALLA IT WORKS. Thank you kind stranger.