• 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 ealva479 · Jun 27, 2018 at 01:20 AM · uiinventory systemitem

Can't seem to get my item hotbar working correctly.

I have my inventory system working for the most part, and now I am working on getting my item hotbar connected, the same as minecraft, so if you pick up an item, it goes to the hotbar first, then once it fills up, it starts going to the actual inventory. Also, you can press the keys 1-6 to select a certain one.

Hotbar Code: using UnityEngine;

public class Hotbar : MonoBehaviour { int space = 6; bool hotbarFull = false;

 public int selectedWeapon = 0;

 public Transform hotbar;
 Inventory inventory;
 HotSlot[] hotSlots;

 [SerializeField]
 public GameObject item1;
 [SerializeField]
 public GameObject item2;
 [SerializeField]
 public GameObject item3;
 [SerializeField]
 public GameObject item4;
 [SerializeField]
 public GameObject item5;
 [SerializeField]
 public GameObject item6;
 
 private bool showItem1;
 private bool showItem2;
 private bool showItem3;
 private bool showItem4;
 private bool showItem5;
 private bool showItem6;

 public void Start()
 {
     inventory = Inventory.instance;
     if (hotbarFull == false)
     {
         inventory.onItemChangedCallback += UpdateHotbar;
     } else if(hotbarFull == true)
     {
         inventory.onItemChangedCallback += FindObjectOfType<InventoryUI>().UpdateUI;
     }

     inventory.onItemChangedCallback += UpdateHotbar;

     showItem1 = true;
     showItem2 = false;
     showItem3 = false;
     showItem4 = false;
     showItem5 = false;
     showItem6 = false;

     hotSlots = hotbar.GetComponentsInC$$anonymous$$ldren<HotSlot>();
 }

 public void Update()
 {

     if (showItem1 == false)
     {
         item1.SetActive(false);
     }
     if (showItem1 == true)
     {
         item1.SetActive(true);
     }
     if (showItem2 == false)
     {
         item2.SetActive(false);
     }
     if (showItem2 == true)
     {
         item2.SetActive(true);
     }
     if (showItem3 == false)
     {
         item3.SetActive(false);
     }
     if (showItem3 == true)
     {
         item3.SetActive(true);
     }
     if (showItem4 == false)
     {
         item4.SetActive(false);
     }
     if (showItem4 == true)
     {
         item4.SetActive(true);
     }
     if (showItem5 == false)
     {
         item5.SetActive(false);
     }
     if (showItem5 == true)
     {
         item5.SetActive(true);
     }
     if (showItem6 == false)
     {
         item6.SetActive(false);
     }
     if (showItem6 == true)
     {
         item6.SetActive(true);
     }

     if (Input.GetKeyDown(KeyCode.Alpha1) && showItem1 == false)
     {
         showItem1 = true;
         showItem2 = false;
         showItem3 = false;
         showItem4 = false;
         showItem5 = false;
         showItem6 = false;
     }
     else if (Input.GetKeyDown(KeyCode.Alpha1) && showItem1 == true)
     {
         showItem1 = false;
         showItem2 = false;
         showItem3 = false;
         showItem4 = false;
         showItem5 = false;
         showItem6 = false;
     }
     if (Input.GetKeyDown(KeyCode.Alpha2) && showItem2 == false)
     {
         showItem1 = false;
         showItem2 = true;
         showItem3 = false;
         showItem4 = false;
         showItem5 = false;
         showItem6 = false;
     }
     else if (Input.GetKeyDown(KeyCode.Alpha2) && showItem2 == true)
     {
         showItem1 = false;
         showItem2 = false;
         showItem3 = false;
         showItem4 = false;
         showItem5 = false;
         showItem6 = false;
     }
     if (Input.GetKeyDown(KeyCode.Alpha3) && showItem3 == false)
     {
         showItem1 = false;
         showItem2 = false;
         showItem3 = true;
         showItem4 = false;
         showItem5 = false;
         showItem6 = false;
     }
     else if (Input.GetKeyDown(KeyCode.Alpha3) && showItem3 == true)
     {
         showItem1 = false;
         showItem2 = false;
         showItem3 = false;
         showItem4 = false;
         showItem5 = false;
         showItem6 = false;
     }
     if (Input.GetKeyDown(KeyCode.Alpha4) && showItem4 == false)
     {
         showItem1 = false;
         showItem2 = false;
         showItem3 = false;
         showItem4 = true;
         showItem5 = false;
         showItem6 = false;
     }
     else if (Input.GetKeyDown(KeyCode.Alpha4) && showItem4 == true)
     {
         showItem1 = false;
         showItem2 = false;
         showItem3 = false;
         showItem4 = false;
         showItem5 = false;
         showItem6 = false;
     }
     if (Input.GetKeyDown(KeyCode.Alpha5) && showItem5 == false)
     {
         showItem1 = false;
         showItem2 = false;
         showItem3 = false;
         showItem4 = false;
         showItem5 = true;
         showItem6 = false;
     }
     else if (Input.GetKeyDown(KeyCode.Alpha5) && showItem5 == true)
     {
         showItem1 = false;
         showItem2 = false;
         showItem3 = false;
         showItem4 = false;
         showItem5 = false;
         showItem6 = false;
     }
     if (Input.GetKeyDown(KeyCode.Alpha6) && showItem6 == false)
     {
         showItem1 = false;
         showItem2 = false;
         showItem3 = false;
         showItem4 = false;
         showItem5 = false;
         showItem6 = true;
     }
     else if (Input.GetKeyDown(KeyCode.Alpha6) && showItem6 == true)
     {
         showItem1 = false;
         showItem2 = false;
         showItem3 = false;
         showItem4 = false;
         showItem5 = false;
         showItem6 = false;
     }
 }
 
 public void UpdateHotbar()
 {
     for (int i = 0; i < hotSlots.Length; i++)
     {
         if (i < inventory.items.Count)
         {
             hotSlots[i].AddItem(inventory.items[i]);
         }
         else
         {
             hotSlots[i].ClearSlot();
         }   
     }      
 }

 public void StopUpdating()
 {
     if (hotSlots.Length >= space)
     {
         hotbarFull = true;
         Debug.Log("Not enough room in hotbar.");
         inventory.onItemChangedCallback += GetComponent<InventoryUI>().UpdateUI;
     }
 }

}

Hotbar Slot Code:

using UnityEngine; using UnityEngine.UI;

public class HotSlot : MonoBehaviour {

 public Image icon;
 public Button removeButton;

 private GameObject player;

 Item item;

 public void AddItem(Item newItem)
 {
     item = newItem;

     icon.sprite = item.icon;
     icon.enabled = true;
     removeButton.interactable = true;
 }

 public void ClearSlot()
 {
     item = null;

     icon.sprite = null;
     icon.enabled = false;
     removeButton.interactable = false;
 }

 public void OnRemoveButton()
 {
     ClearSlot();
     Inventory.instance.Remove(item);
     //Instantiate(item, player.transform.position + new Vector3(0, -3, 0), player.transform.rotation);

 }

 public void UseItem()
 {
     if (item != null)
     {
         item.Use();
     }
 }

}

Inventory:

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

public class Inventory : MonoBehaviour { private GameObject player;

 #region Singleton
 public static Inventory instance;

 private void Awake()
 {
     if(instance != null)
     {
         Debug.LogWarning("More than one instance of Inventory found!");
         return;
     }
     instance = t$$anonymous$$s;
 }
 #endregion

 public delegate void OnItemChanged();
 public OnItemChanged onItemChangedCallback;

 public int space;

 public List<Item> items = new List<Item>();

 public bool Add (Item item)
 {
     if(!item.isDefaultItem)
     {
         if(items.Count >= space)
         {
             Debug.Log("Not enough room.");
             return false;
         }
         items.Add(item);

         if(onItemChangedCallback != null)
             onItemChangedCallback.Invoke();
     }
     return true;
 }

 public void Remove(Item item)
 {
     items.Remove(item);
     
     if (onItemChangedCallback != null)
         onItemChangedCallback.Invoke();
     print(item.name);
 }

}

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

144 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Grid Inventory : Move Item & Slot Highlighting 2 Answers

[C#]Inventory script help. 3 Answers

Inventory AddItem help 1 Answer

Problem with if statements relating to images 1 Answer

Removing item from inventory not updating correct based on order 1 Answer


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