- Home /
Do un-active game objects still use memory?
My title is a little vague, so let me explain...
I created an ramp with some "addons" attached to it in my 3D program. The ramp is the parent, then it has some arrows pointing in the direction you need to go as a child, and a support beam underneath as another child. The whole thing is exported as a single FBX file, but not collapsed so I can access the children individually in Unity.
I wrote a small script that is attached to the GameObject, which lets me assign the arrows and the support beam as "addons", and makes it possible to "deactivate" or "activate" those "addons" to make them visible or invisible.
They key word here is "activate", as in gameObject.active = false/true. It does't destroy or instantiate the addons game objects, it just hides them. My question is, since the "addons" are not being destroyed but just hidden, is my object just as expensive with the addons invisible then with them visible? Do I need to "destroy" and "instantiate" them if I want to save memory and speed?
Thanks in advance for any clarification on this, and thanks for your time! Stephane
finally, someone with a question (I think) I can answer.
Activating and deactivating is more efficient if you have a lot of update functions attached to whatever it is you are trying to activate/deactivate.
Answer by PetterDK · Mar 06, 2012 at 04:22 PM
I am currently wondering about this very same question, but can't seem to find a straight answer anywhere.
So far what I've gathered is that when you use the gameObject.active toggle, none of the components on that gameObject will receive any updates. That means that you are saving performance in the sense that the inactive object is not considered at runtime, before it is activated. The object DOES exist in memory though when using this approach.
When using Instantiate and Destroy, the object is not present in memory before it is instantiated. While that means saving memory usage, instantiating the object seems to be a more expensive process than activating an inactive object. Especially if it's a complex object, you will likely take up some performance when instantiating.
To answer your question, then: No, your objects are not as expensive when they are hidden (gameObject.active = false), than if they are visible (gameObject.active = true). This is because the components within the object are not receiving any updates from Update, FixedUpdate or LateUpdate, and of course the fact that it's not being rendered.
If you want to save memory per se, then yes – you will have to use Instantiate/Destroy.
"but can't seem to find a straight answer anywhere."
want a straight answer?
Heres one straight up!
(through it may or may not be incorrect)
you are correct. except for the line:
If you want to save memory per se, then yes – you will have to use Instantiate/Destroy.
one line of code is better than 2. (I should think anyway)
Yea, I guess I did put a straight answer in there myself :) Totally unintentionally, of course..
But yes, I think you are right in saying that one line of code is better than two..
Oh, and ronronmx – If you found my answer useful, please send some karma my way.. Thx :)
Thanks for your explanation! So yeah, an unexisting gameobject is still far better tahn an inactive one, but I guess that for menus and GUI elements you can't really not just disable and enable objects