Any method to hide objects is only affecting the prefabs and not the active instances

First of all, I’m very new to Unity (like 3 weeks) so maybe I’m just missing some crucial detail I’m unaware of.
I’m working on an item system in which when the player picks up an item, the item on the environment gets destroyed, and a usable prefab of the item is instantiated in the player’s hand, that works fine, but I want the player to switch which item they’re currently holding with the mouse scroll wheel so I need those instances to be able to disappear and reappear, but apparently SetActive() and other methods I tried like setting scale to zero or just moving their position never affects the objects instantiated at runtime and only their prefabs in the folder. Do I really need to have all the items already on the scene so they can work?


Instantiate creates a copy of the gameObject and returns a reference to it. So you should do it like this:

heldItem = Instantiate(item, gameObject.transform);

Although I think you may find it easier to make the item a child of the player instead of instantiating a new item and destroying the original.

2 Likes

Note that not only need you use the return value of the Instantiate call, you will also have to call Destroy() on that instance when you drop it again.

Also, Show is the better opposite for Hide than Unhide. You don’t shout to the stranger to “Unhide yourself, whereever you are!” :wink:

While I appreciate the retro sentiment, that font it almost illegible by modern standards. You’re doing your eyes and your brain a disservice. A source code font primarly has to serve the coder’s ability to quickly scan text, not to serve an aesthetic feel. The right font helps you avoid simple but hard to spot mistakes like 1 vs I vs i vs l vs |.

Compare this with a modern font like Fira Code:

… or “even” Source Code Pro:

Okay, I understand.
About the font though, I’m already quite used to it and the one I use was even a custom version downloaded from GitHub with ligatures for coding lmao

Welcome! Just beware that you are tackling one of the hardest problems: an inventory / item system integrated with your world.

I’m not saying this to discourage you, only to help you calibrate your expectations of the high level of effort required.

I keep a handy crib sheet of the things you may need to consider, depending on what features you intend to ultimately implement.

These things (inventory, shop systems, character customization, dialog tree systems, crafting, ability unlock systems, tech trees, etc) are fairly tricky hairy beasts, definitely deep in advanced coding territory.

The following applies to ALL types of code listed above, but for simplicity I will call it “inventory.”

Inventory code never lives “all by itself.” All inventory code is EXTREMELY tightly bound to prefabs and/or assets used to display and present and control the inventory. Problems and solutions must consider both code and assets as well as scene / prefab setup and connectivity.

If you contemplate late-delivery of content (product expansion packs, DLC, etc.), all of that has to be folded into the data source architecture from the beginning.

Inventories / shop systems / character selectors all contain elements of:

  • a database of items that you may possibly possess / equip
  • a database of the items that you actually possess / equip currently
  • perhaps another database of your “storage” area at home base?
  • persistence of this information to storage between game runs
  • presentation of the inventory to the user (may have to scale and grow, overlay parts, clothing, etc)
  • interaction with items in the inventory or on the character or in the home base storage area
  • interaction with the world to get items in and out
  • dependence on asset definition (images, etc.) for presentation
    → what it looks like lying around in the world? In a chest? On a shelf?
    → what it looks like in the inventory window itself?
    → what it looks like when worn by the player? Does it affect vision (binoculars, etc.)
    → what it looks like when used, destroyed, consumed?

Just the design choices of such a system can have a lot of complicating confounding issues, such as:

  • can you have multiple items? Is there a limit?
  • if there is an item limit, what is it? Total count? Weight? Size? Something else?
  • are those items shown individually or do they stack?
  • are coins / gems stacked but other stuff isn’t stacked?
  • do items have detailed data shown (durability, rarity, damage, etc.)?
  • can users combine items to make new items? How? Limits? Results? Messages of success/failure?
  • can users substantially modify items with other things like spells, gems, sockets, etc.?
  • does a worn-out item (shovel) become something else (like a stick) when the item wears out fully?
  • etc.

Your best bet is probably to write down exactly what you want feature-wise. It may be useful to get very familiar with an existing game so you have an actual example of each feature in action.

Once you have decided a baseline design, fully work through two or three different inventory tutorials on Youtube, perhaps even for the game example you have chosen above.

Breaking down a large problem such as inventory:

If you want to see most of the steps involved, make a “micro inventory” in your game, something whereby the player can have (or not have) a single item, and display that item in the UI, and let the user select that item and do things with it (take, drop, use, wear, eat, sell, buy, etc.).

Everything you learn doing that “micro inventory” of one item will apply when you have any larger more complex inventory, and it will give you a feel for what you are dealing with.

Breaking down large problems in general:

The moment you put an inventory system into place is also a fantastic time to consider your data lifetime and persistence. Create a load/save game and put the inventory data store into that load/save data area and begin loading/saving the game state every time you run / stop the game. Doing this early in the development cycle will make things much easier later on.

Various possible inventory data structures in Unity3D:

1 Like

Thanks, that’s quite a lot of info, I’ll save that for reference somewhere.
What I’m currently doing as my first project is kinda simple, it’s just about letting the player pick up things to use during a single playthrough with no saving for now, but I’ll keep that in mind as I want this project to be a base for future ones so I gotta leave room for expandability.