I am wondering how exactly to use prefabs in Unity for my discretely coordinate-based game. Should they only be for gameObjects that are exactly the same? Or can they have small variations?

Example: a treasure chest is interactable and it holds items, so I would make it an empty gameObject, add an interactable component and then an itemContainer component and then drag and drop it into my prefabs folder and delete the instance in the scene.

But the question remains, if I make this treasure chest a prefab, how do I edit the itemContainer component of this treasure chest? Moreover, how would I serialize it for later use, and organize my data? Would I have to use external data files and reconstruct the ItemContainerComponent, just for a treasure chest? That sounds like a lot of work.

If I edit the itemContainer component as a prefab, then all treasure chests will have the same items. Obviously this is not feasible.

Do I drag my treasureChest prefab from the prefabs folder and then simply drop it in the game? That's how most people use it, right?

I would prefer not to just drag and drop the prefabs, because I want a more industrialized approach, and I do want some way of saving what is in each treasure chest. I can't for the life of me figure out a good way. Help appreciated!

EDIT: I see the link provided that marks this as a possible duplicate. However, none of the answers in that link are clear enough to be useful to me, e.g., the "factory." It would be nice if someone could explain the factory idea in more concrete terms.


Prefabs are for anything you want to create at run time. After it has been instantiated, you can modify any and all properties without affecting other "copies."

For instance, I have a scrolling list of upgrades in my game, each one had a name, cost, and picture. But I don't want to hand-create 100 menu items (god forbid the costs or names change!). Instead I create a prefab and aList<Upgrade> object. Then at run time, I iterate over the list, clone a prefab, imagery it into the scrolling area, set the text and sprite vales to the details in the Upgrade object, and bam, the game creates the scrolling menu for me.


A prefab is identical when instantiated. That doesn't mean it can't immediately show variety.

For example, in one of my games I have a prefab of a house that is used multiple times. The house is made from modular parts (wall segments, doors, roof parts, etc.). My prefab includes a script that in its Start() method picks one of its children at random and keeps it, while calling Destroy() on the others. This one, every time I instantiate a house, it will have a random wall segment in this place, a random set of props around it, sometimes a small and sometimes a big window here, etc.

There are other ways to make prefabs vary. For simple values, like the number of gold coins in a treasure chest, you can simply define a range (lowest and highest amount) in the prefab and in Start() call Random.Range() to set the actual demand for this chest. Voila, every chest now has a random amount of coins between your values.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.