Performance -- Activating (Enable/Disable) vs. Instantiating/Destroying objects

I know this subject has already been covered (I read some of the threads on it), but could not find anything about specific “case scenarios” of just how many objects would be best for each scenario.

My situation is this: I have a player character (first-person VR) that can pick up / collect items as they explore a dungeon. As they do so, the objects are placed in hand (childed to a specific attach point transform object on the hand to ensure appropriate placement), but they are also added to Inventory.

The player can obviously then swap out items/weapons equipped in hand. In such a scenario, would it be preferable to have all objects remain attached to hand and enable/disable them as they are equipped, or better to instantiate one each time (and destroy when new item equipped?)

How many items are you thinking about, here? If it’s only a dozen or two for an inventory then I’d go with having them always exist, because that’s almost certainly easier.

Anyway, the reason you haven’t found a definitive, one-size-fits-all answer is that there isn’t one. Also, believe it or not, performance isn’t the only consideration. Often it’s not even the most important consideration.

Personally, my tendency in Unity is to pre-create as much as I can. If possible I pre-create everything. In part that is to avoid the costs of on-the-fly instantiation. But, it’s also in huge part because you can only directly reference something that exists, and that’s a massive ease of use consideration for Unity’s workflows - it’s the basis of the Inspector system, for starters.

5 Likes

How long is a piece of string? Profile your specific use case, guessing won’t get you anywhere.

1 Like

Can you expand upon this @angrypenguin ? Do you mean front load everything needed - for instance - at the beginning of each level, or am I misunderstanding?
Thanks -

Yep, that’s exactly what I mean. It’s not always possible, but when it is possible it’s what I generally try to do.

1 Like

Reusable /object pooling is always faster… Any Instantiate in your code, will generate huge spikes.

in my experience its faster to move the transform to an hidden place and call it back when needed…
you will need to disable its components though

1 Like