Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Blink · Aug 25, 2013 at 04:39 PM · guiinventoryitem pickup

Hotbar and item pickups?

So ive been looking around sometime now for a tutorial that shows this and nothing has come up that i have found useful.

What i am wanting to do is have a hotbar at the bottom of the screen which has 10 boxes that are all numbered in ascending order. If the player walks over something that he is able to pickup, then it will be sent onto the hotbar where you can equipt or use it by pressing the number of the box the item is in.

Please can someone help me out? I hope this picture helps explaining things.

alt text

hotbar.jpg (1.3 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by numberkruncher · Aug 25, 2013 at 05:03 PM

There is a free inventory system in the asset store which might help to get you jump started: https://www.assetstore.unity3d.com/#/content/10384

As you will appreciate, an inventory is essentially a GUI with some sort of backing data structure. Here is a very simple example of how you might go about achieving this with the standard Unity GUI. You will need some sort of data representation to describe an inventory item. There are a number of ways to achieve this, this is just one way!

Note: The following has not been tested, but should demonstrate the basic idea :)

Find similar UnityScript translation of following example here: http://pastebin.com/VSQYMnKS

Inventory Item Behaviour

Attach script to each inventory item, and perhaps even use as a base class when additional functionality is needed.

 using UnityEngine;
 public class InventoryItem : MonoBehaviour {
     // Icon texture for GUI.
     public Texture2D icon;
     // Label for GUI.
     public string iconLabel;

     // other item data which you may need...

     public virtual void Activate() {
         // Activation logic for inventory item.
         gameObject.SetActive(true);
     }

     public virtual void Deactivate() {
         // Deactivation logic for inventory item.
         gameObject.SetActive(false);
     }
 }

Inventory Behaviour

Create game object to manage your inventory and then associate inventory items using inspector.

 using UnityEngine;
 public class Inventory : MonoBehaviour {
     // Fixed length array of items, though you may want to use
     // the generic `List` class if you wanted varying length.
     public InventoryItem[] items = new InventoryItem[10];

     // Represents the active inventory item.
     public InventoryItem activeItem;

     // Temporary content instance.
     private GUIContent _itemButtonContent = new GUIContent();

     void OnGUI() {
         Color restoreColor = GUI.contentColor;

         GUILayout.BeginArea(new Rect(0, 0, Screen.width, 70));
         GUILayout.BeginHorizontal();

         // Draw buttons for inventory items.
         foreach (InventoryItem item in items) {
             if (item == null) {
                 // Just show a blank non-clickable button.
                 GUI.enabled = false;
                 GUILayout.Button(GUIContent.none);
                 GUI.enabled = true;
             }
             else {
                 // Show clickable button.
                 _itemButtonContent.image = item.icon;
                 _itemButtonContent.text = item.iconLabel;

                 // Highlight active item.
                 GUI.contentColor = (item == activeItem)
                     ? Color.red
                     : restoreColor;

                 if (GUILayout.Button(_itemButtonContent))
                     ActivateItem(item);
             }
         }

         GUILayout.EndHorizontal();
         GUILayout.EndArea();

         GUI.contentColor = restoreColor;
     }

     public void ActivateItem(InventoryItem item) {
         // Don't bother, this item is already active!
         if (item == activeItem)
             return;

         DeactivateItem();

         // Active inventory item!
         activeItem = item;
         item.Activate();
     }

     public void DeactivateItem() {
         // No item is active, bail!
         if (activeItem == null)
             return;

         activeItem.Deactivate();
         activeItem = null;
     }
 }
Comment
Add comment · Show 14 · Share
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
avatar image Blink · Aug 25, 2013 at 05:10 PM 0
Share

In the second script on line 11 I get, "A new expression requires () or [] after type" Not to sure what this means?

avatar image numberkruncher · Aug 25, 2013 at 05:13 PM 0
Share

Sorry that was a typo (fixed), like I said the above has not been tested so there may be a couple of $$anonymous$$or syntax errors ;)

avatar image Blink · Aug 25, 2013 at 05:42 PM 0
Share

Thanks for that the Gui side of things is perfect :) but now I need the items to become active when their button is active. Could you help with this?

Im thinking if all the items are held children of the main camera but marked as inactive, when their button is clicked it is marked as active and can then be used?

avatar image numberkruncher · Aug 25, 2013 at 07:22 PM 0
Share

Each InventoryItem has an activate and deactivate method which is automatically called upon selecting the GUI button. I have updated the example so that it activates/deactivates the item game object also. Was this what you meant?

avatar image Blink · Aug 25, 2013 at 09:42 PM 0
Share

Yeah that's great thanks! As for picking items up is there a way to only be able to select an item if you have picked it up from the ground?

When the player comes into contact with an item which can be picked up, it adds it onto the hotbar where you can then select it.

Show more comments

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

16 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

Related Questions

Scripting help (sending messages?) 1 Answer

Script Not Working 0 Answers

Adding Item object to Inventory List 0 Answers

Is there any easy way to keep buttons inside of box? (c#) 1 Answer

Simple Backpack "Pick up and drop" 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