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 raycosantana · Sep 30, 2013 at 08:42 AM · inventorygameobjectsstore

How to store gameobjects to create an inventory.

Hello, I want to create an inventory system but I dont know how to start, I cant seem to figure out how should I store the gameobjects the player collects and how to stack them if the item is already there. I dont need the code for full inventory system, just some hints to put me in the right track.

This is an example of an item I created:

 using UnityEngine;
 using System.Collections;
 
 public class Potion : MonoBehaviour {
     public int PotionValue;
     public AudioClip[] PotionSounds;
     public int Cantidad = 0;
     // Use this for initialization
     
     void Start () {
     
     }
 public void AddCantidad(int exCantidad){
         Cantidad += exCantidad;
     }
     
 public void use(){
     GameObject Player = GameObject.FindGameObjectWithTag("Player");
     Player.SendMessage("AddjustCurrentHealth", PotionValue);
     audio.PlayOneShot(PotionSounds[(Random.Range(0,PotionSounds.Length))]);
     Destroy(gameObject);
     }
 }

Thanks a lot

Comment
Add comment · Show 1
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 Artifactx · Jan 17, 2015 at 05:57 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 including storing of GameObjects

3 Replies

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

Answer by hamstar · Sep 30, 2013 at 10:08 AM

There would be a few ways to do this, but I would do something like this. Take a look the tutorial on Lists I think you might find it helpful. I probably wrote too much code, but sometimes it's easier to understand than paragraphs of explanation.

IItem.cs

 // Interface, containing only signature (design), not implementation
 interface IItem { 
   // classes that implement Item should define this method
   void UseItem();
 }

Potion.cs

 // Implements the Item interface. Attach this to potion gameobject.
 public class Potion : MonoBehaviour, IItem { 
   public AudioClip[] PotionSounds;
   public int PotionValue;
     
   public void UseItem() {
     GameObject Player = GameObject.FindGameObjectWithTag("Player");
     Player.SendMessage("AddjustCurrentHealth", PotionValue);
     audio.PlayOneShot(PotionSounds[(Random.Range(0, PotionSounds.Length))]);
     Destroy(gameObject);
   }
 }

InventoryManager.cs

 // This behaviour script would be attached to the player
 public class InventoryManager : MonoBehaviour {
   // stores items picked up
   private List<IItem> itemsInInventory = new List<IItem>();
     
   void Update() {
     // check for use input to use item here, or in OnGUI, and call Use()   
   }
     
   void OnCollisionEnter(Collision col) {
     if (col.gameObject.tag == "Potion") {
       // We cast to IItem to match our list type. 
       // We can do this because Potion implements IItem.
       // This means we could store different types of items in the list, 
       // as long as they implement IItem.
       itemsInInventory.Add((IItem)col.gameObject.GetComponent<Potion>()); // adds new item to end of list
     }
   }
     
   void Use() {
     if (itemsInInventory.Count > 0) {
       // execute UseItem method of first item in list
       itemsInInventory[0].UseItem(); 
       // remove item from list since it has been used
       itemsInInventory.RemoveAt(0); 
     }
   }
 }

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 raycosantana · Sep 30, 2013 at 12:19 PM 0
Share

Thanks a lot, I will look into Lists

avatar image
0

Answer by hans_vesthardt98 · Sep 30, 2013 at 09:07 AM

I am not an expert, but i think you can store the gameObjects in your player prefs list and then make sure that when there is 1 of the gameObject, and you collect another you will have 2 of them. I think you can do this by saying that if you collect an item, it should set the value of the item to +1. Not an expert though, this is just a guess!

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 raycosantana · Sep 30, 2013 at 09:15 AM 0
Share

You cant actually, Playerprefs can only store Float, int and string variables

avatar image hans_vesthardt98 · Sep 30, 2013 at 09:24 AM 0
Share

Oh well, as i said not an expert :p

avatar image
0

Answer by screenname_taken · Sep 30, 2013 at 09:43 AM

I will be making a game that requires an inventory but i haven't tackled it yet. But the way i thought about going around in doing it is to that the inventory will be an array that holds an int (the number of times i found that item) and a string (item's name.) The game would spawn that item in the screen when the player would click on the GUITexture. I'd have an empty gameobject as a child in the player's hand, and the game would take that transformation as the point to spawn the game object that was in the inventory.

Comment
Add comment · Show 6 · 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 raycosantana · Sep 30, 2013 at 10:04 AM 0
Share

As far as I know, you cant make an array that takes both an int and a string at the same time, is an int array(int[]) or an string array (string[]) both at the same time cant be.

avatar image screenname_taken · Sep 30, 2013 at 10:08 AM 0
Share

Not true. You can make your custom class to hold the number of items, and to have the item's info in it. And then do an array of that class.

avatar image raycosantana · Sep 30, 2013 at 12:19 PM 0
Share

Still, Arrays are not resizable so unless you put all objects in the array before hand, you cant add or subtract an object from an Array.

avatar image screenname_taken · Mar 02, 2014 at 01:58 PM 0
Share

You'd have to make a new array that is bigger or smaller and then copy over the stuff you want i guess, and then delete the first one? (Can you completely delete an array from memory? i only found on clearing it.)

avatar image hamstar · Mar 02, 2014 at 04:38 PM 0
Share

You can use:

 System.Array.Resize(ref array, newLength);

What this will actually do is create a new array in memory and copy all of the content from the original array to the new one. You will still use the same variable (array name) to access the "new" array afterwards.

Because of this copying, resizing an array is quite expensive and so you should avoid doing it frequently. If you know how many types of items are going to be in your game, you already know how big the array needs to be at the start, and you won't need to resize it.

Of course you could use a List. A list is better in cases where you don't know before hand how many items you want to store. List items are stored in memory differently to arrays, and are a bit slower to use. However, for most cases I doubt you will notice.

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

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

Storing the status of a GameObject. 2 Answers

PlayFab: KeyNotFoundException: The given key was not present in the dictionary. 0 Answers

Cannot convert from 'int' to 'PlayFab.ClientModels.ItemInstance' 1 Answer

Storing transforms from objects in Array 1 Answer

First person pick up object and store into inventory? 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