Setting the parent of a transform which resides in a Prefab...

I have an instantiated prefab that I want to change the parent of when the player does something.
The instantiation:

Instantiate(itemButton, inventory.slots[i].transform, false);

the re-parenting:

for (int i = 0; i < inventory.slots.Length; i++)
            {
                if (inventory.isFull[i] == false)
                {
                    inventory.isFull[i] = true;
                    Instantiate(itemButton, inventory.slots[i].transform, false);
                }
            }

I have tried many of the online solutions, but none of the worked. Keep in mind that when it is instantiated, there are multiple instances.

When I try this, I get the error message “Setting the parent of a transform which resides in a Prefab Asset is disabled to prevent data corruption”

Thank you for noticing!

Line 6 is making a whole fresh copy.

Also, in your first code block you are not keeping what Instantiate made for you.

Keep what Instantiate returns, use that to change parenting.

Keep firmly in mind that these things (character customization, inventories, shop systems) are fairly tricky hairy beasts, definitely deep in advanced coding territory. They 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

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

  • can you have multiple items? Is there a limit?
  • 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.

Or… do like I like to do: just jump in and make it up as you go. It is SOFT-ware after all… evolve it as you go! :slight_smile:

Breaking down a large problem such as inventory:

Well they are using the overload that parents the copy. The issue here is that inventory.slots*.transform is probably a prefab, so they’re trying to parent the copy to a prefab, not a scene level asset.*
As I’ve mentioned over and over again, OP needs to make sure they’re trying to parent this to a scene object, not a prefab.
A prefab != a scene object, but a scene object can be a copy of a prefab. I’m not even sure of how they’ve set things up to cause this issue. One would imagine the instance of this inventory UI running in a scene would be the one generating and parenting these instances to itself, but it seems some outside agent is trying to do this by referencing the prefab, which is the completely wrong way to architect this.

I understand what the issue is–that I am trying to deal with a prefab and not a scene object–but I don’t know any other methods of targeting a scene asset besides using “gameObject,” as I am a noob. Again, I apologize for being a noob.

A prefab and not a scene object*. Lets just make this clear. A prefab is not the same object as what’s in the scene. They are separate instances with no connection with one another aside from the fact that the latter is a copy of the former.

We already know you can’t mutate prefabs during run time. Nothing will change that. No amount of asking this question is going to change that, otherwise you’re just running around in circles.

The issue here is an architectural one. Why is the inventory running in the scene not in charge of instancing these UI elements and adding them to itself? Why is something else trying to add objects to it? Those questions are the answers to your problem. Make the inventory UI in charge of itself, and just make it take requests to add new items to itself.

Obviously you can’t do this by referencing a prefab of the inventory UI. Instead, look at other patterns, such as a singleton pattern or event broker pattern.

When something gets instantiated, it becomes a scene object and a reference is returned from Instantiate.

It kinda looks like:

GameObject TheThingIWantMade = Instantiate<GameObject>( Prefab, OptionalParentTransform);

I know this. This is not what I’m trying to do. I’m trying to manipulate a specific scene object that is instantiated from a prefab.

This is helpful. I will use this to try and come up with something. Thanks!

If I had to guess, I would guess that what this refers to:

is actually the prefab of the inventory, NOT the instantiated prefab in the scene.

It is very likely you need to accept what Instantiate returns and use that reference in place of the variable inventory above.