• 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 SPIGS · Jan 20, 2014 at 10:40 PM · inventorysystembasicadvanced

Inventory system?

I need help creating an inventory system much like Minecraft's (minus the crafting). I have somewhat of an idea of what I have to do, but all the tutorials I look at or articles I read that explain how to make an inventory system don't seem to fully explain everything. It has really been bugging me...

 static public Dictionary<> inventory; // For items in inventory

 public class Item
 {
 //For Item creation
 }

I need some explanation, and I need to know how to make it work! Answers Appreciated.

Comment
Add comment · Show 4
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 DynamicPrgm · Jan 21, 2014 at 01:56 AM 0
Share

I happen to be working on something similar to that myself, and you need a way to display the content of the inventory. The way I display the inventory is by creating a 2-dimensional array that holds the item data and looping through each part of the array creating a button for each iteration of the 2D array, and displaying the name on the button. The reason it is 2D is because there is a width and a height of the grid of the buttons. You should learn the Unity GUI stuff.

I haven't finished yet, so I still need to figure out how to drag the buttons and drop the items and use them and whatever.

avatar image Josh707 · Jan 21, 2014 at 02:37 AM 0
Share

You can use a variable to hold the class data and use the buttons if statement and loop index to check if the value is or isn't null to place/pick up the GUI item (swap between null and that item), if that variable isn't null draw some GUI element at the mouse position to show that you're holding it. I'm not sure how you'd make it drag and drop though.

avatar image DynamicPrgm · Jan 21, 2014 at 02:43 AM 0
Share

Yes, I was thinking that if you're holding the button, then you can take the position of the mouse on the screen and draw the button at a point where the mouse pointer is at the middle of the button by finding the exact middle of the button and working backwards from there to set the position of the button so the middle is right where the mouse is.

avatar image Artifactx · Jan 17, 2015 at 05:56 PM 0
Share

Here is a great tutorial, that $$anonymous$$ches you how to create an inventory system from scratch, with the most common operations: https://www.youtube.com/watch?v=$$anonymous$$LaGkc87dDQ

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Negagames · Jan 20, 2014 at 11:05 PM

You have a start, most of what you need done is done with that snippet actually, from here on all you have to do is define item properties (ex. Name, Type ect...) and create some UI (user interface )handeling

If you're not already familiar with it, Unity has a great API where you can learn to use all of the different functions provided w/ the unity engine. Here is a link the the API on the subject of GUI: http://docs.unity3d.com/Documentation/ScriptReference/GUI.html

Comment
Add comment · Show 3 · 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 SPIGS · Jan 20, 2014 at 11:36 PM 0
Share

Thanks. I've been meaning to look into the GUI.

avatar image Josh707 · Jan 21, 2014 at 01:17 AM 0
Share

It depends on how you want your inventory to work but the concept is simple. If you have one array for the 'bag' of items, you can have another for equipped/hotkeyed ones like you would find in $$anonymous$$inecraft. If you want single slots then just have single variables of the item class. As @Negagames hinted above, loop through those arrays/read variables for the GUI. You can give the class a texture variable for the GUI to read.

Adding/removing items is just adding/removing an element to the array, a gameobject can have a script with the item variables for the new element you're adding. If you make it 2 dimensional then you can pretty easily use 2 for loops to make a grid of buttons and use the current index to select/grab/etc. an item from the array.

'Using' them is similar to the GUI, read whatever item you need to via hotkey or whatever, and based on the variables in that class call certain functions that can use other class properties, like food and how much health it gives you.

avatar image SPIGS · Jan 21, 2014 at 02:56 AM 0
Share

I think have something. I just need to finish it, I'll work on it tomorrow.

avatar image
0

Answer by YoungDeveloper · Jan 20, 2014 at 11:28 PM

Before writing an actual code, i would like talk about couple things, like what is overall basic idea from game creation perspective itself.

Map designers, quest creators and many others, these people mostly never code when creating maps, quests, place npc etc. They have ready custom built tools for doing that, all what they do is move sliders, search through lists and place/write (not code).

So, before actually creating the game experience itself, we need to make sure that we the right tools for doing that process, it will be taking a long time to create the map, so each helpful thing for the map designers can save tens of hours.

One way would be storing ready prefabs in one folder, but imagine if prefab amount is around 4 thousand, considering all little props, items, mobs etc. Artist will drive nuts working like that. So, custom editors is the only option, you could sort specific item lists under different categories, also show how they look or will look. That would be much more easier to work with and focus on map building than trying to find specific item for 10 minutes.

It means, each item, Quest, NPC, monster, gun, attachment, etc should be somehow recognizable (specific number or something), but how? If in simple case all what it has is a tag ?

Creating a structured list for each type of item would be good logic, for example one list for pistols, one for magazines ans so on. When the list is created you can easily access it from any point in your game and custom editors. Which means if you update you food list, your custom editor will automatically will be updated.

So basically when having these lists, you can spawn anything and anywhere And track things via Log.

So first, we create the list (array) which will hold information about the items. This script should be on player itself.

 public class Items : MonoBehaviour{
 
     //We create a Node, possible information each this current item  will hold
     [System.Serializable] //Without this next class "Item" wouldn't not show in inspector
     public class Item{
         public string name; // its name
         public int weight; //just showing the idea
     }
 
     public Item[] allItems;
 
     //This is just for showing how it might work
     public InfoAboutItem(int index){ //This will be called from player
         Debug.Log("You picked up " + allItems[index].name + " and it weights " + allItems[index].weight);
    }
 }

After attaching script you basically now can create item list in any size you want, and each can contain lots of information.

Now we need a script which will be located on item itself, so we would know what item is that.

 public class ThisItem : MonoBehaviour{
     public int itemIndex; // remember that first element is 0 not 1
 }

Yes, that's it :-).

Now the script which will find the item. Also on player.

 public class Player : MonoBehaviour {

     void OnTriggerEnter ( Collider col ){
         if (col.tag == "Item") { //All previous items in game can be under one tag "Item"
             GetComponent<Items>().InfoAboutItem(  col.GetComponent<ThisItem>().itemIndex  );
 
             Destroy(col.gameObject);
         }
     }
 }

 

  • Set the list in size of 1 (in inspector), in first element fields, enter its name and weight.

  • Place ThisItem script on the pickable item, set index to 0, tag should be "Item"

After triggering the item the text will appear and it will be destroyed.

This is the basic idea how you can create very advanced item in game structures.

A bit of a lecture, but i hope it was interesting to ones just starting :).

Comment
Add comment · Show 1 · 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 SPIGS · Jan 21, 2014 at 12:58 AM 0
Share

$$anonymous$$y game is in 2d and I converted it so that it would work within my game, though however it didn't work. I do however understand it better. Things like this have always been a hassle for me. I try to play around with it more, if I can't come up with anything, I'll just get something from the asset store to help.

avatar image
0

Answer by darkal · Jan 21, 2014 at 01:41 AM

i recommend true/false statements such as if something is in slot 1 the slot is false so it check slot 2 i like to use a count to keep track of things that like lets the script check itself

Comment
Add comment · 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

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

25 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

Related Questions

Particle System, change the velocity by script 1 Answer

weapon system script 1 Answer

each enemy have different damage amount, how to calculate the hp remain??? 1 Answer

Making this rotation instant... 2 Answers

Very simple inventory script... 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