This is a question about performance on iPhone. Due some issues I have replaced "Instantiate" for my enemy objects with gameObject.active = true; As far as I know, Instantiate and Destroy may be quite expensive functions for iPhone and are causing hiccups when Instantiating complex objects. But I have the same effect when I set my enemy active with Trigger function.
So - am I right that Instantiating and activating objects are the same thing for performance?
The differences between an active GameObject and an inactive GameObject are:
- The inactive GameObject’s attached MonoBehaviours won’t receive most Unity messages. This includes update calls (FixedUpdate, LateUpdate, Update) and any messages sent by SendMessage or BroadcastMessage.
- An inactive GameObject won’t interact with physics.
- An inactive GameObject won’t show up in the array returned by Object.FindObjectsOfType(GameObject).
You can still directly reference an inactive GameObject, as Statement mentioned, and you can directly call methods on it and its components. You could even directly call Unity message methods if they’re public, though that might get confusing. In summary, the difference between inactive and destroyed is the difference between hiding your computer in a closet with spotty Internet access and chucking it into a black hole.
GameObject.active isn’t the quickest property on Earth, but setting it should be much faster than Destroying and Instantiating a new GameObject, and it won’t waste any memory (unless you use SetActiveRecursively, which consumes a tiny bit of memory to get the enumeration it iterates over.)
Most of the time they are the same. When you disable an object, it is "removed" and to place it back in takes same time as instantiating a new one.
No, but close enough.
For example, if you hold a reference to a game object or a component attached to it and destroy the object, those references turn into null, where inactive game objects just don't execute any code from the engine. You may still call functions on any attached scripts.
As long as you are aware of this difference, it is just about the same, and I believe it is a good approach on reducing overhead. I ain't truthfully sure.
- How much performance you gain or lose, you have to test for yourself.