Is it cheaper to deactivate a game object or change its alpha, or other?

Hi guys,

I am making a space shooter and want a simple optimization question. I have a pool of flame particles that trail behind the ships, and when a ship is destroyed, I’m wondering if it would be cheaper for me to se their alphas to 0 and let their animations continue, or just deactivate the game objects?

The ships themselves are also part of the field and get re-used, so they will keep the flame particles with them.

The question really is, is it cheaper to turn the alpha on and off of a bunch of objects, or to deactivate and reactivate a bunch of objects?

Or is there a third and better solution, ie, turning off the sprite renderer or some other option?

Thanks for your time

Destroying an object releases its memory, means it no longer uses any CPU time (since none of the Update() functions on its attached components will be called), but causes the garbage collector to have to reclaim the memory and, if you want to use it again, means it will have to be reinstantiated. Only use this for objects that you’re certain you don’t want to instantiate again.

Deactivating a gameobject keeps its memory allocated, but no longer uses any CPU time (Update() etc. on deactivated objects is not called), and creates no additional work for the garbage collector. Reactivating it is a trivial operation.

Disabling the sprite renderer keeps the object’s memory allocated, and continues to use CPU time since Update() etc. will still get called, but doesn’t cause any work for the renderer since nothing gets drawn to screen.

Setting alpha to zero continues to use all the memory, CPU, and rendering resources as normal - it’s just you can’t see the object. Transparent objects still generate just as much work for the rendering pipeline - it’s just that at the point a pixel is drawn to the screen (normally at the very last step of the fragment shader), the blend mode means it has no influence on the final colour. I can’t think of any situation where you’d want to do this.

Unless you have a very specific reason to keep your memory footprint to an absolute minimum, I’d recommend deactivating the object for the vast majority of cases.