- Home /
What is the difference between these three ways of making an object disappear?
gameObject.renderer.enabled=false
gameObject.active=false
GameObject.Destroy(gameObject)
These three are each make the object disappear from the scene. What is the difference between them?
EDIT: for clarity
Answer by Daniel-Brauer · Aug 26, 2010 at 04:11 PM
For InvokeRepeating...
1) disable whole game object - InvokeRepeating does NOT stop
2) disable just the script component - InvokeRepeating does NOT stop
For coroutines...
3) disable whole game object - coroutine >>> DOES <<< stop
4) disable just the script component - coroutine does NOT stop
In all four cases, the repeat is "eliminated", it is NOT paused. If you again SetActive(true), in all four cases, the repeat does NOT continue where it left off.
Simply use OnDisble() and the similar routines if you want custom behaviour on disabling.
Behaviour.enabled and Renderer.enabled stop an attached Renderer or Behaviour (MonoBehaviour and number of other components) from running their update code. In the case of Renderers, it stops them from showing. You can turn this variable on and off from code or use the checkbox next to the Behaviour in the Inspector. Note that not all Components are Behaviours or Renderers, so (for instance) Colliders can't be disabled.
GameObject.active is like enabled, but for all Components on a GameObject. Instead of just stopping Update() and rendering callbacks, it stops all callbacks and coroutines. GameObject.active is the only way to turn off attached Colliders. You can turn this variable on and off from code or by using the checkbox at the top of the GameObject inspector.
Object.Destroy() causes the permanent deletion of its target. This can be a Behaviour, Component, GameObject, or anything else that inherits from Object. If you destroy a GameObject, all of its attached Components will also be destroyed. Destroy() is the same as selecting something in the Hierarchy and deleting it. Destroy() is irreversible.
In this context, it would be cool if daniel could also tell the difference on gameObject.SetActiveRecursively(true/false); :-)
That's just a way of setting GameObject.active on a hierarchy of objects. It doesn't have any special behaviour or side-effects.
hi jkgd .. see the exact answer in the bold lines added to the answer.
Answer by AliAzin · Aug 26, 2010 at 04:03 PM
GameObject.Destroy(gameObject) delete the game object, gameObject.active=false disable the game object and all of its components, gameObject.renderer.enabled=false disable only the renderer so you can't see the object but all of its components do work.
Answer by sigs · Feb 17, 2012 at 06:47 AM
it would be nice to know more specifically what changing Renderer.enabled does. For some reason, toggling GameObject.enabled back and forth can take a LOT of time; no very good explanation is given in the docs where the time is wasted at.
For instance, what kind of CPU/GPU traffic should I expect from toggling Renderer.enabled? Will VBOs be re-uploaded every time? Are collisions enabled when renderer is disabled? What about skinning calculations?
it couldn't be simpler - if you disable the renderer, that disables the renderer! the renderer DRAWS THE OBJECT ON THE SCREEN. it's that simple. everything else (scripts, colliders, physics, UI, whatever - anything at all you have on the game object - of course keeps running. it's just that it is "invisible".
Your answer
Follow this Question
Related Questions
Button to create object??? 1 Answer
How To Keep GameObjects From Enabling Again? 2 Answers
Active Prefabs 1 Answer
Why does script continue executing when turned off or destroyed? 2 Answers
Destroy and instantiate far objects 2 Answers