Which is better for performance, removing or disabling a component?

I am making a building game. i have a buildable gameobject, and when I instantiate it, I need access to a script that controls certain options. Once the object is finalised, these scripts are no longer needed. In this particular case, there could be ++thousands++ of these objects in the gameworld.

Will the performance overhead of continuously running these objects with the component disabled outweigh the performance cost of using remove component to get rid of them completely?

In theory, I would imagine that the best way to do it would be to remove the component, but would like some advise from anyone who may have some solid advice they can give.

This all depends on how frequently you want to remove the components.

As it’s a building game and the destruction of the component only happens every now and then when the user is finished tweaking the object, it wouldn’t hurt at all.

If you had some kind of logic that destroyed components frequently, for example every frame or even several components per frame, you should think about an alternative.

You said there may be thousands of objects in theory, so If you simply disabled the components, you’d waste pretty much memory in the end, even though some platforms wouldn’t bother at all. Keep it clean and easy.

This being said, i want to mention another way, maybe even a better approach:

Have a manager-like object that provides the script(s) with the functionality you need and as soon as you instantiate a new gameObject, register that one to the script. You’ll ever need only one instance of the script, that simply takes the current object and offers your functionality.

Another advantage of that is, that you could store a list of favourite settings / presets that could be applied per button-click, but that depends on what you actually want to tweak. That would make sense if you had modular objects and allow options like changing the textures, colors, size, … of course it wouldn’t make that much sense for positioning. But that’s up to you, be creative. :wink:

Could you post some code? I have a feeling you could vastly optimize your game by settings each object as a temporary variable and using a foreach loop to control the parameters of the scripts attached. That being said, there is a credible post here that you should check out if you really do need a script on thousands of objects at once.

Good luck!

Thanks for the answer, that is pretty much what I was looking for. Since originally posting this, I have decided to rewrite the structure, and tame some of my ideas, so that I may be able to get away without using a script on the object at all, but we’ll see how that goes.

Thanks again.