Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Dragunas · Feb 10, 2021 at 05:33 PM · playerinventoryinventory systemitemdetect

how to detect wich item i have in my inevntory

i found a video on youtube wich showed me how to make a inventory system. I made some changes but now i dont understand how to check wich items are in my inventory. In my ItemObj scrip i tried the method

"if (this.GetComponent().sprite.name == "Weapon")"

but if i try to use something like a campfire it gets a null refrence exception. Can somebody help me to find out how to detect wich items i have in my inventory?

temDataBase script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
  
 public class ItemDataBase : MonoBehaviour
 {
     public List<Item> items = new List<Item>();
 
     void Awake()
     {
         GetItemByID();
     }
 
     
 
     void GetItemByID()
     {
         items.Add(new Item("Glock", "Waffe", "Hoffentlich", 0));
         items.Add(new Item("M4A1", "Ein 8 Kaliber Gewehr", "Weapon", 1));
     }
 
     public Item GetItemByID(int id)
     {
         foreach (Item itm in items)
         {
             if(itm.itemId == id)
             {
                 return itm;
             }
         }
 
         
         return null;
         
     }
 }

Item scirpt:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
  
 [System.Serializable]
 public class Item {
     public int itemId;
     public string itemDescription;
     public Sprite itemSprite;
     public string itemName;
     public string itemSpriteName;
  
     public Item(string name, string description, string spriteName, int id)
     {
         itemName = name;
         itemDescription = description;
         itemSpriteName = spriteName;
         itemSprite = Resources.Load<Sprite>("ItemIcons/" + spriteName);
         itemId = id;
         
     }
 
     public Item()
     {
 
     }
  
     
 }

Inventory script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Inventory : MonoBehaviour
 {
 
     public int slotAmount = 11;
     public GameObject slotPrefab;
     public GameObject slotParent;
     public GameObject InventoryObject;
     public GameObject itemPrefab;
 
     private MouseLook mouseLook;
 
     private ToolTip tooltip;
 
     public List<GameObject> slots = new List<GameObject>();
     
 
     private ItemDataBase itemDatabase;
 
     public KeyCode inventorykey = KeyCode.Tab;
 
     public bool isShown = false;
     
 
     // Start is called before the first frame update
     void Start()
     {
 
         CreateSlots(slotAmount);
 
         mouseLook = GameObject.FindObjectOfType<MouseLook>();
 
         itemDatabase = GameObject.FindGameObjectWithTag("ItemDataBase").GetComponent<ItemDataBase>();
 
         tooltip = GameObject.FindGameObjectWithTag("Tooltip").GetComponent<ToolTip>();
 
     }
 
 
     void Update()
     {
         
         if (Input.GetKeyDown(inventorykey))
         {
             isShown = !isShown;
 
             if(isShown == true)
             {
                 Cursor.visible = true;
                 tooltip.ToggleTooltip(false);
                 Cursor.lockState = CursorLockMode.None;
                 mouseLook.mouseSensitivity = 0f;
 
             }
             else if (isShown == false)
             {
                 
                 Cursor.lockState = CursorLockMode.Locked;
                 mouseLook.mouseSensitivity = 900f;
                 Cursor.visible = false;
             }
 
         }
         if(isShown == true)
         {
             InventoryObject.SetActive(true);
 
         }
         else if(isShown == false)
         {
             InventoryObject.SetActive(false);
 
 
         }
     }
     // Update is called once per frame
     void CreateSlots(int slotAmount)
     {
         for (int i = 0; i < slotAmount; i++)
         {
             slots.Add(Instantiate(slotPrefab));
             slots[i].transform.SetParent(slotParent.transform);
         }
     }
 
     public void AddItem(int id)
     {
         Item itemAdd = itemDatabase.GetItemByID(id);
         for (int i = 0; i < slots.Count; i++)
         {
             if(slots[i].transform.childCount <= 0)
             {
                 GameObject itemInstance = Instantiate(itemPrefab);
                 itemInstance.transform.SetParent(slots[i].transform);
                 itemInstance.transform.localPosition = Vector2.zero;
                 itemInstance.GetComponent<Image>().sprite = itemAdd.itemSprite;
                 break;
             }
 
            
         }
         
     }
 
    
 }

ItemObj script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class ItemObj : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler, IPointerEnterHandler, IPointerExitHandler
 {
     private Transform startParent;
 
     private CanvasGroup canvasGroup;
 
     private Campfire campfire;
 
     private Item item;
 
     private PickUp pickUp;
  
     public Inventory inventory;
 
     private ItemDataBase itemDataBase;
 
     private ToolTipButton toolTipB;
 
     private ToolTip tooltip;
     private ToolTipButton toolTipButton1;
     private ToolTipButton toolTipButton2;
 
     private Food foodItem;
     private PlayerMovement playerMovement;
     private Interact interact;
 
 
     private bool inItem = false;
 
     private void Start()
     {
         interact = GameObject.FindObjectOfType<Interact>();
         foodItem = GameObject.FindObjectOfType<Food>();
         playerMovement = GameObject.FindObjectOfType<PlayerMovement>();
         inventory = GameObject.FindObjectOfType<Inventory>();
         canvasGroup = GetComponent<CanvasGroup>();
         toolTipB = GameObject.FindObjectOfType<ToolTipButton>();
         pickUp = GameObject.FindObjectOfType<PickUp>();
         
 
         tooltip = GameObject.FindGameObjectWithTag("Tooltip").GetComponent<ToolTip>();
         toolTipButton1 = GameObject.FindGameObjectWithTag("TooltipButton1").GetComponent<ToolTipButton>();
         toolTipButton2 = GameObject.FindGameObjectWithTag("TooltipButton2").GetComponent<ToolTipButton>();
 
 
         startParent = transform.parent;
     }
 
     private void Update()
     {
 
         if (Input.GetKeyDown(KeyCode.Mouse1) && inItem == true)
         {
             tooltip.ToggleTooltip(true, Input.mousePosition);
 
             toolTipButton1.actionMethod = UseItemButton;
 
             toolTipButton2.actionMethod = DropViaItemButton;
         }
 
         Ray ray = Camera.main.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
         RaycastHit hitInfo;
         if (Physics.Raycast(ray, out hitInfo))
         {
             if (hitInfo.collider.tag == "Campfire")
             {
                 if (Input.GetKeyDown(KeyCode.E))
                 {
                     if (this.GetComponent<Image>().sprite.name == "Weapon")
                     {
                         Destroy(gameObject);
                         campfire = hitInfo.collider.GetComponent<Campfire>();
                         inventory.AddItem(0);
                         
                     }
                 }
 
             }
         }
 
     }
 
     private void UseItemButton()
     {
         if (this.GetComponent<Image>().sprite.name == "Hoffentlich")
         {
             Debug.Log("you used it");
 
             useItemTwo();
         }
 
         if (this.GetComponent<Image>().sprite.name == "Weapon")
         {         
             Debug.Log("you used it");
 
             useItem();
         }
     }
 
     private void DropViaItemButton()
     {
         if (this.GetComponent<Image>().sprite.name == "Hoffentlich")
         {
             DropItem();
 
             Debug.Log("You dropped your item!");
         }
 
         if (this.GetComponent<Image>().sprite.name == "Weapon")
         {
             DropItem();
 
             Debug.Log("You dropped your item!");
         }
     }
 
 
     void useItem()
     {
         interact.TestHunger2();
 
         Destroy(gameObject);
     }
 
     void useItemTwo()
     {
         interact.TestHunger();
 
         Destroy(gameObject);
     }
 
 
 
     void DropItem()
     {
         Destroy(gameObject);
         
     }
 
 
 
 
 
     public void OnBeginDrag(PointerEventData eventData)
     {       
         transform.position = eventData.position;
 
         transform.SetParent(transform.parent.parent);
 
         canvasGroup.blocksRaycasts = false;
     }
 
     public void OnDrag(PointerEventData eventData)
     {
         transform.position = eventData.position;
     }
 
     public void OnEndDrag(PointerEventData eventData)
     {
         GetSlotInfo();
 
         canvasGroup.blocksRaycasts = true;
     }
 
 
     private void GetSlotInfo()
     {
         GameObject[] slots = GameObject.FindGameObjectsWithTag("Slot");
 
         foreach (var slot in slots)
         {
             if(slot.GetComponent<Slot>().isSelected == true)
             {
                 if(slot.transform.childCount > 0)
                 {
                     ItemObj otherItem = slot.transform.GetChild(0).GetComponent<ItemObj>();
 
                     Transform otherItemParent = otherItem.startParent;
                     otherItem.startParent = startParent;
                     startParent = otherItemParent;
 
                     otherItem.transform.SetParent(otherItem.startParent);
                     otherItem.transform.localPosition = Vector2.zero;
                 }
                 else
                 {
 
                     startParent = slot.transform;
 
                 }
                
                 break;
             }
         }
 
         transform.SetParent(startParent);
         transform.localPosition = Vector2.zero;
     }
 
     public void OnPointerEnter(PointerEventData eventData)
     {
         inItem = true;
     }
 
     public void OnPointerExit(PointerEventData eventData)
     {
         inItem = false;
     }
 }
 

i would be really gratefull if somebody could help me

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

139 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Hey, i can't figure out, what my problem is. I want to Add something into the player's Inventory, but before that it should check, whether there is an Item with the same name in it, or not and if yes, if the maxAmount for that slot is reached. 0 Answers

[C#]Inventory script help. 3 Answers

Inventory AddItem help 1 Answer

How i can make a script-made button interactable? 0 Answers

How to combine items in inventory with words 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges