- Home /
How to implement pickups that can go in a inventory
So I want to have a pickup that exists in the world, and when you pick it up, it goes into your inventory. My question is, what's the best way to represent this in Unity?
Ideally I'd like this inventory item to be a prefab that I can drop in the world. That's all well and good, but my issue comes up when the player picks it up.
When it's in the player's inventory, there's no object in the world anymore, its just stored in an array for inventory objects. Of course, if the player drops it, it will again be in the world.
This feels like it will use two different kinds of objects, a GO with a mesh (etc) and a non-mesh object (not sure what that's called?) when it's in your inventory. BUT I DONT LIKE THIS ROUTE. It doesn't feel right to me. Id prefer it to be one thing, although, if I can't have it that way, I understand.
Any suggestions on how to handle this?
Answer by LucasMeijer · Aug 07, 2011 at 07:06 AM
There are many ways to skin this cat. One way would be:
write a MonoBehaviour called "InventoryPickup". make it check if the player is closeby. if it is closeby, have it set the .enabled of the gameobject that has the MeshRenderer to false.
at the same time, it could call a function on a global "InventoryManager" object to notify it that the item was picked up. Something like:
GetPlayer().GetInventoryManager().ItemGotPickedUp(this);
the inventory can then maintain a list of "InventoryPickups", and refer to that list whenever it wants to check if a certain item is in the inventory, or when the inventory needs to be visualized in some way. Lets say there is a 2d on screen visualization of the inventory. In such case, the InventoryPickup script could have a
public Texture2D icon;
where you could assign the 2d icon to be used in the interface.
Thanks Lucas. I have the basics of an inventory down, it's just the turning of a real-world object into a "in memory only" object that's screwing me up.
Sounds like your suggestion is to leave the pickup GO where it is, just turn its physical properties (mesh, collider) off?
Answer by Waz · Aug 07, 2011 at 07:07 AM
Keep it in the world, just inactive. Then you can manipulate it (use up charges, use it's component's properties, etc.), and have that persist if the player drops it. You might even just move it well off-screen, that way it can still have an Update, etc.
So if in code I have a reference to it, but I deactivate it, I can still access it from code?
Yes. You cannot use Send$$anonymous$$essage, and GetComponentsInChildren defaults to ignoring it (but has an option). Plus, as stated, it does not Update (but you'd probably not want it to either).
But otherwise, you can interact with it freely from code.
Answer by FTheCloud · Aug 07, 2011 at 07:07 AM
Why don't you just instantiate another one when the player picks it up and that'll be what the player will hold. When the player puts it down just destroy the one that you instantiated when the player picked it up and the one in the inventory would drop down into the world. It would all be seamless.
That's sufficient if the item doesn't have changeable properties.
Your answer
Follow this Question
Related Questions
Pick up gameobject and go to inventory, 1 Answer
Pick Up objects when crosshair is over object 1 Answer
adding inventory items is half-working 0 Answers
Inventory adds an item only once. 0 Answers
I wanna make a simple inventory system that can hold three objects But I can't get it to work. 0 Answers