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
1
Question by Nercoe · Jan 23, 2013 at 11:19 AM · inventoryefficient

Is this a good way to script an inventory system?

Ok guys I am just wondering if this is an efficient way of implementing an inventory system.

Basically, I will be working with booleans. Say I have a boolean that is "hasSword = true" if the boolean is true, print the 2d graphic in the inventory system and give the user the ability to wield the sword for example:

 function Start()
      {
       sword.gameObject.SetActiveRecursively(false);
    }
 }
 
 function Update()
      {
       if (weapon == 1 && hasSword == true)
      {
       sword.gameObject.SetActiveRecursively(true);
    }
 }

Is this an efficient way of doing what I want or is there a better way?

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 Meater6 · Jan 23, 2013 at 11:41 AM 0
Share

I made an inventory script a few days ago... It was quite complex. It was made for a top-down hack-n-slash game. Would you like me to post it? It may be of some use to you, or not. Either way, it is long and will take time to read and understand.

avatar image Nercoe · Jan 23, 2013 at 11:47 AM 0
Share

That would be great! If you wouldn't $$anonymous$$d, it would be interesting to read over :)

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

Hey, this tut might help you out: https://www.youtube.com/watch?v=$$anonymous$$LaGkc87dDQ

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

Hey here is a great tutorial, that $$anonymous$$ches you how to create an inventory system from scratch with the new unity 4.6 ui: https://www.youtube.com/watch?v=$$anonymous$$LaGkc87dDQ

2 Replies

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

Answer by Meater6 · Jan 23, 2013 at 08:32 PM

Ok, well here is the script.

DISCLAIMER: Users may only use this code as a learning device. This work shall not be used in any commercial or non-profit product, unless given permission by the license owner (Shadonyx Entertainment). Violation of these terms may be subject to lawsuit. This is not meant to be a finished product.

Don't plagiarize! That means no copying and saying you made it! Don't do anything illegal! Oh, and don't give this to random people! Just to be safe, it's pretty much for your eyes only Nercoe. I hope other users have some discretion and decency. For some reason, I have a feeling that this will be used/plagiarize by hundreds of new users. -_- If you avoid all that, go ahead!

There's a few things that you should keep in mind (other than possible legal issues). This script is placed on an empty game object. Items are other game objects (with an Item script, but for this example that is irrelevant). Items that are place inside the inventory become children to the inventory and become deactivated. Vice-versa happens when you drop an item.

 //Copyright 2013, Shadonyx Entertainment
 //Written by Jordan Abrahams
 //Strictly for learing use only! Do not plagiarize!
 #pragma strict
 
 class Inventory extends MonoBehaviour {
 
     var slots = new GameObject[20];
     
     var rows : int = 5;
     var collumns : int = 4;
     
     var totalWeight : float = 0.0;
     var gold : int = 0;
     
     private var player : GameObject;
     
     function Start () {
         player = GameObject.FindWithTag("Player");
     }
     
     function AddItem (obj : GameObject) {
         var item : Item = obj.GetComponent.<Item>();
         
         for(var i : int = 0; i + 1 <= slots.length; i++) {
             if(item.stack && slots[i]) {
                 if(slots[i].name == obj.name) {
                     slots[i].GetComponent.<Item>().stackCounts += item.stackCounts;
                     Destroy(obj);
                     break;
                 }
             }
             if(!slots[i]) {
                 slots[i] = obj;
                 totalWeight += item.weight;
                 obj.transform.parent = transform;
                 obj.SetActive(false);
                 
                 var abilityArray = new Array(player.GetComponent(AbilityCaster).abilities);
                 abilityArray.Add(item.ability);
                 player.GetComponent(AbilityCaster).abilities = abilityArray;
                 break;
             }
             else if(i + 1 >= slots.length) {
                 Debug.Log("Your inventory is full. Drop something to open a slot.");
                 break;
             }
         }
     }
     
     function DropItem (obj : GameObject) {
         for(var i : GameObject in slots) {
             if(i == obj) {
                 var itemScript = i.GetComponent(Item);
                 var icon = itemScript.icon;
                 var trans = player.transform;
                 var pos = trans.position;
     
                 i.transform.parent = null;
                 i.transform.position = pos + trans.TransformDirection(Vector3(0, 0.5, 0.5));
                 
                 i.SetActive(true);
                 
                 totalWeight -= itemScript.weight;
                 if(icon) {
                     Destroy(icon);
                 }
                 i.SetActive(true);
                 i = null;
             }
         }
     }
     
     function MoveItem (from : int, to : int) {
         if(slots[from]) {
             if(slots[to]) {
                 var switchedSlot : GameObject = slots[to];
                 slots[to] = slots[from];
                 slots[from] = switchedSlot;
             }
             else {
                 slots[to] = slots[from];
                 slots[from] = null;
             }
         }
         else {
             Debug.LogWarning("Attempting to move a missing item; there is no item at slot: " + from);
         
         }
     }
     
     function ActivateItem (item : GameObject) {
         for(var i : GameObject in slots) {
             if(i == item) {
                 i.SendMessage("Activate");
             }
         }
     }
 }

I mentioned before, please remember that this is not a finished product. It is also not guaranteed to be bug free, nor to be the most efficient way. I spent many long hours working on my inventory system. Think of this as a act of empathy.

Comment
Add comment · Show 4 · 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 Nercoe · Jan 24, 2013 at 01:54 AM 0
Share

Fantastic, I am going to haul through this and understand it in full when the morning comes. Thank you very much and it has helped me understand more complex inventory systems.

avatar image Meater6 · Jan 24, 2013 at 06:32 AM 0
Share

If you have any questions about it, come back here and ask me. :)

avatar image mavii_ege · Jan 12, 2015 at 10:11 AM 1
Share

going to sell this

avatar image Meater6 · Jan 17, 2015 at 05:53 PM 0
Share

Go ahead, it's been 2 years. :P

avatar image
0

Answer by ryba · Jan 23, 2013 at 11:33 AM

It depends on how complex inventory will be.

If there wont be wide variety of items, but only booleans if there is an item equipped, meaning that there wont be 2 or more different swords, just one sword as weapon, then you could create one InventoryController componenent, and assign it to your player.

Inside this component just set bool fields for every possible item, that will indicate if item is equipped + add methods like isSwordEquipped, isArmorEquipped etc...

Then, in your inventorty window just call those methods to check if items icon should be displayed or not.

If you need more complex equipment system, then nobody will tell you how to implement it best, if you wont describe how your equipment system should work.

Comment
Add comment · Show 2 · 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 Nercoe · Jan 23, 2013 at 11:38 AM 0
Share

Just to clarify I didn't ask for someone to implement it for me, I am confident with my program$$anonymous$$g skills, I was just asking for advice which is what this is for... Right? Anyway, thanks for the advice I guess I will go down the more complex route. I was just wondering if this way would be sufficient.

avatar image ryba · Jan 23, 2013 at 11:49 AM 0
Share

I didnt tell i want to implement it for you, but just throw an advices how to implement that :) Thats what you asked for, right ? If you still want help just describe how this system should work and then i could give some advices.

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

13 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

Related Questions

how can i make this code more efficient for my inventory system 0 Answers

Key and Door Tutorial Help? 2 Answers

Inventory system Help 2 Answers

Creating an interactable inventory? 1 Answer

Inventory stack system problam 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