Advertisement
Pro_Unit

ItemPickup

Nov 18th, 2021
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.91 KB | None | 0 0
  1. using System.Collections.Generic;
  2.  
  3. using UnityEngine;
  4.  
  5. public class ItemPickup : MonoBehaviour
  6. {
  7.     public Item item;
  8.     private Inventory inventory;
  9.     private InventoryUI inventoryUI;
  10.  
  11.     private void Start()
  12.     {
  13.         inventory = FindObjectOfType<Inventory>();
  14.         inventoryUI = FindObjectOfType<InventoryUI>();
  15.     }
  16.  
  17.     public void PickUp()
  18.     {
  19.         Debug.Log("Picking up " + item.name);
  20.  
  21.         Item foundedItem = inventory.items.Find(inventoryItem => inventoryItem.name == item.name);
  22.        
  23.         if (foundedItem != null)
  24.         {
  25.             foundedItem.amount = 5;
  26.             inventoryUI.UpdateUI();
  27.             inventory.Add(item);
  28.         }
  29.        
  30.         Destroy(gameObject);
  31.     }
  32. }
  33.  
  34. public class InventoryUI : MonoBehaviour
  35. {
  36.     public void UpdateUI() { }
  37. }
  38.  
  39. public class Inventory : MonoBehaviour
  40. {
  41.     public static Inventory instance;
  42.     public List<Item> items;
  43.  
  44.     public void Add(Item item) { }
  45. }
  46.  
  47. public class Item : ScriptableObject
  48. {
  49.     public int amount;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement