- Home /
I just need a little advice on performance optimization.
I have a bunch of different characters (and intend to add more), the players can select 2 at a time. Is it best if I use DontDestroyOnLoad and destroy or should I create a scene with the character container and player (which works together and works by setting active the selected characters) that I keep persistent? I'm getting conflicting arguments in my searches some people saying it makes no difference with either or they are using a totally different approach that I don't think would work for my game.
i am not really sure if i am understanding your issue. you are asking if its should be better to spawn/destroy the character or have a pool of inactive characters and activate the different characters when needed?
Yes, that's essentially the question I'm still new to the lingo so please forgive me. I just wanted to know which is best for performance, because it would seem that a big chunk of data (all of my characters) going around every scene might not be efficient.
"Performance" comes in many flavors. Instantiate and Destroy is generally worse for the framerate than keeping them alive in memory. First of all if the objects are relatively large / complex the instantiate call would be slower as more data / objects need to be created allocated which takes time. Likewise destroy and the necessary garbage collection is also worse the larger the objects are (large in terms of memory, not diameter).
On the other hand if you're on a low memory device such as an older mobile phone you could run out of memory if you always keep everything in memory. So it really depends on the case. I once worked on a mobile racing game with a car selection menu. Inside that menu we had all cars instantiated on some sort of podests and you could swipe through them in two dimensions (left right the brand / model, up down the tier). Of course the cars weren't that highpoly meshes (since we target mobile) though we had probably about 30 to 40 cars in the scene. Instantiating them as you swipe would have produced serious lag spikes and probably a bit of delay.
What makes sense in your case depends on many factors The major two factors are: the target platform (and the average available memory) as well as how much memory your games and your objects need.
Your answer
![](https://koobas.hobune.stream/wayback/20220612200145im_/https://answers.unity.com/themes/thub/images/avi.jpg)