- Home /
Code Optimization Question
Hi, all! this is just a quick code optimization question. If you don't know the answer, then I understand ;)
I have a script that randomly picks 1 of 3 asteroid prefabs to spawn, picks a random position within a certain radius, gives it a random speed, random size, etc., then instantiates it.
Well I have 4 empties all with this script in different places.
Would it be more processor heavy to have 4 different empties with this script, all doing independent calculations, Or would it be more heavy to have ONE object simply choose from an array of those empties' transforms, and spawn them x many times as quickly?
Thanks!- YA
well generally in video games you make a POOL of objects, leave them offscreen, and just use them as needed. Generally, never Instantiate during actual gameplay. Just build your POOL at the, say, begtinning of a level. Here is a massive discussion
http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html
Pls vote it up if useful to you! :)
Answer by mstevenson · Nov 17, 2012 at 08:33 AM
You generally want to do as much repetitive work as possible within a loop in a single script. Each MonoBehaviour in the scene incurs a small amount of overhead, and this can become quite expensive if you have a large number of behaviours in the scene.
Well, there's not much I can do about it if all they are doing is constantly rotating, moving forwards, and checking its Z position for when it needs to be destroyed.
Your answer
Follow this Question
Related Questions
Is Instantiating bullets/many objects always bad for performance? 1 Answer
Instantiate not working? 0 Answers
Instantiate Code Issue 0 Answers