- Home /
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
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
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);
}
}
}
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!
You cant actually, Playerprefs can only store Float, int and string variables
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.
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.
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.
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.
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.)
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.
Your answer
Follow this Question
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