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 Trollvahkiin · Oct 16, 2013 at 10:12 PM · guiarrayinventoryinventory system

How to start an inventory system?

Hey so I have two scripts, they are very basic. I never tried this before so I thought I would start simple. One is for the actual inventory and the other for items. You should be able to make you what it all means. I've hit a point where I have more questions on how to continue than actual idea to do. If someone could please at least guide me on what I would need to do a simple inventory. Right now I'm looking to be able to pick up an item (Disable renderer and child to player) and place in the inventory slots, also icons for them. Simply be able to click an item to select and then click one of the options in a separate window like use, drop or equip.

  • How could I store my items

  • How can I select then and then pick an option

  • How would I put the item in a not already taken space

  • How could I make the inventory, how can I use an array to make up a GUI out of it.

Yes I've searched it up videos but not many of them show what I need or talk about details.

The code:

 #pragma strict
 
 //Inventory
 
 static var inventoryArray : int[] = [0,0,0,0,0];
 var displayInventory : boolean = false;
 
 var unzipSound : AudioClip;
 var zipSound : AudioClip;
 
 private var barLength = 0.0;
 
 function Start()
 {
     barLength = Screen.width / 7;
 }
 
  
 function Update() 
 {
     if(Input.GetKeyDown(KeyCode.I))
     {
         displayInventory = !displayInventory;
         
         if(displayInventory)
         {
             audio.PlayOneShot(unzipSound);
         }
         else
         {
             audio.PlayOneShot(zipSound);
         }
     }
 }
 
 function OnGUI()
 {
     if(displayInventory)
     {
         GUI.Box(Rect (0,0,100,50), inventoryArray[0]); //the part where I wanted to display the array
     }
 }

 #pragma strict

 //item
 
 var inventory : Inventory;
 
 function OnTriggerEnter(col : Collider)
 {
     if(col.tag == "Player")
     {
         gameObject.active = false;
         
         inventory.inventoryArray[0]++;
     }
 }
Comment
Add comment · Show 2
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 ccrh · Aug 17, 2014 at 02:39 AM 0
Share

Just a suggestion, as it is what I am doing with my inventory system: Ins$$anonymous$$d of parenting the object to the player, why not add the object to the inventory then destroy the item on the ground? Then if you go to drop it you can just instantiate a new copy of that item below the player's feet and remove it from the inventory list. Not particularly sure if it would be better than parenting but it looks cleaner (to me) when coding it in.

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

This might help you out: https://www.youtube.com/watch?v=$$anonymous$$LaGkc87dDQ

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Cherno · Oct 17, 2013 at 08:24 AM

"How could I store my items"

Just put the item object into the next free inventory array slot. (InvArray[NextFreeSlot] = ItemObject)

"How can I select then and then pick an option"

In OnGUI(), if the inventory should be shown, iterate through all slots of the inventory array and if the slot is not empty, draw the icon by accessing the icon graphic of the item gameobject stored in that slot. Either way, draw a button at the required coordinates where the slot should be shown and add some if statements when it's left clicked: If the slot isn't empty, pick up the object (make the slot null), ... and if it's right clicked, display an options dialogue etc...

"How would I put the item in a not already taken space"

Before picking up an item, call a small function that finds the next free inventory slot and returns this slot's number. You can then use this number to let the game know at which position of the inventoy array your new item should be stored.

"How could I make the inventory, how can I use an array to make up a GUI out of it."

See the second question's answer.

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

Answer by b1gry4n · Aug 17, 2014 at 03:32 AM

Highly recommend lists.

Remember to put System.Collections.Generic; at the top of any scripts using List

To answer your question. Start with creating a Class for the individual items:

 [System.Serializable]
 public class Item{
 public int identifier;
 public string itemName = "";
 public string description = "";
 
 ...ETC...
 
 }

Next, you need a way to store these items. create a database:

 public List<Item> itemDatabase= new List<Item>(); // this is the core of the database, after weve created an item, we add that item to the list (see below when i make an item)

 void Awake(){
 Item sword = new Item();
 sword.identifier = 1; // the identifier has to be unique to each item
 sword.itemName = "Sword"; 
 sword.description = "Hit things with it" // this could be used in a tooltip
 itemDatabase.Add(sword); // weve added the item to our database
 ...ETC...
 }


Now you need a way to iterate through the itemDatabase youve created...

 public Item GetItem(int identifier){ //remember, we set up unique identifiers above
 for(int i = 0; i< itemDatabase.Count; i++){
 if(itemDatabase[i].identifier == identifier){
 return itemDabase[i];
 }
 }
 return null;
 }

Now weve created individual items, an item database, and a way to find those items. How you set up the UI is up to you. I would break it up into two things. The inventory and the inventory slots. Each has their own script.

-Inventory manages the slots. -Invnetory slots manage the items/UI.

setting up a slot...

 public Item item; // the item this slot contains
 
 //Add other parameters here for dragging/dropped/etc


adding an item to a slot...

 public List<InventorySlot> slots; // add the slots to this list
 
 public void InventoryAddItem(Item theitem){
 //Forloop through the slots, specify how you want the item dropped. Is the slot full? Is the item stackable?
 for(int i= 0; i< slots.Count; i++){
 //if the slot isnt full
 slots[i].item = theItem;
 }
 
 }

The inventory would have a list of available slots. If you are using NGUI you can utilize the "OnDrop" "OnPress" events to manage picking up/moving/dropping items from slot to slot.

ForLoop is your best friend. Good luck

http://unity3d.com/learn/tutorials/modules/beginner/scripting/loops

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.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

How to go around creating inventory with new GUI (4.6 Beta)? 1 Answer

Which is better for an inventory system, Array or List? 0 Answers

Click on a button that is created post start. 0 Answers

GUI focus control - inventory system issue 1 Answer

scroll bar inventory with slot count for equip items 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