Hi, I’m fairly new to Unity and was wondering whether instantiating and destroying objects vs setting to active or not was a good idea. I made an extremely primitive pickup / hold/ drop system with a hotbar with 5 slots and you can switch between, depending on the item in it, it will create that object, put it into your hand (blank transform) and when you scroll onto the next slot destroy it, instantiate the next object etc. I was wondering if i ever made this prototype larger scale would it be benefical to refactor to setactive() ?
If you feel it’s easier to code then use Instantiate for now and if later on you start noticing a spike when cycling through the items because some of them are compex then you can pool them.
There’s a good chance you’ll lose interest before you finish developing the game and so don’t stress yourself over small details early on.
Thanks for the tip :). tbh i just made this as a little test to my skills rather than a polished game, i was just curious for the future. the items are super low poly anyways.
Best way to learn… go do it more!
Keeping only the instantiated copies around is probably going to be the least memory, while keeping all of them instantiated and just setting them active / inactive is going to be the fastest performance, most likely.
But early on it won’t matter to you.
Instead, you should focus on just using Unity as straightforward as you can, benefitting from the fact that Unity3D is a BEAST of a game engine. It will do amazing things and put up with amazing abuse, so focus on having fun learning first.
Finally, if you do have a performance issue:
For all performance and optimization issues, ALWAYS start by using the Profiler window:
Window → Analysis → Profiler
Generally optimization is:
- avoid doing the thing that is slow (easiest)
- do it fewer times and store its result
- do the slow thing less frequently over time
- do the slow thing when nobody cares much (during level loading or in another thread, etc.)
- find a faster way to do the thing (hardest)
DO NOT OPTIMIZE “JUST BECAUSE…” If you don’t have a problem, DO NOT OPTIMIZE!
If you DO have a problem, there is only ONE way to find out: measuring with the profiler.
Failure to use the profiler first means you’re just guessing, making a mess of your code for no good reason.
Not only that but performance on platform A will likely be completely different than platform B. Test on the platform(s) that you care about, and test to the extent that it is worth your effort, and no more.
Remember that you are gathering information at this stage. You cannot FIX until you FIND.
Remember that optimized code is ALWAYS harder to work with and more brittle, making subsequent feature development difficult or impossible, or incurring massive technical debt on future development.
Don’t forget about the Frame Debugger window either, available right near the Profiler in the menu system.
Notes on optimizing UnityEngine.UI setups:
When you are investigating a performance concern, you may even need to create custom tooling or scripts to clearly expose the issue and help you reason about possible solutions. This may even involve making special instrumented builds of your game capable of running on the actual problematic target hardware.
At a minimum you want to clearly understand what performance issues you are having:
- running too slowly?
- loading too slowly?
- using too much runtime memory?
- final bundle too large?
- too much network traffic?
- something else?
If you are unable to engage the profiler, then your next solution is gross guessing changes, such as “reimport all textures as 32x32 tiny textures” or “replace some complex 3D objects with cubes/capsules” to try and figure out what is bogging you down.
Each experiment you do may give you intel about what is causing the performance issue that you identified. More importantly let you eliminate candidates for optimization. For instance if you swap out your biggest textures with 32x32 stamps and you STILL have a problem, you may be able to eliminate textures as an issue and move onto something else.
This sort of speculative optimization assumes you’re properly using source control so it takes one click to revert to the way your project was before if there is no improvement, while carefully making notes about what you have tried and more importantly what results it has had.
“Software does not run in a magic fairy aether powered by the fevered dreams of CS PhDs.” - Mike Acton
I never destroy/instantiate unless there’s a good reason (like re-creating a list of GO’s, need to empty it first). Hiding/showing objects isn’t a good reason to do this imo. No need to use SetActive() either, as this stops everything in the object. Just turn the renderer on and off. This way the scripts, colliders, etc are still active.