Empty gameobject just to hold scripts?
Would it be a bad practice to create an empty game object for the sole purpose of holding lots of generic scripts?
If it's ok, Is there anything to consider when doing this?
Examples scripts that the gameobject would hold.
All UI interaction and animation
Global timer
Scene loading
Character controller
Events
Answer by rutter · Nov 06, 2015 at 05:56 PM
Would it be a bad practice to create an empty game object for the sole purpose of holding lots of generic scripts?
Not at all. That sounds like a great idea to me!
Out of the scripts you mentioned, I'd usually rather attach a character controller to the character it's controlling. Other than that, though, all of those things could reasonably be global, either to a scene or in some cases even across multiple scenes.
For truly global objects such as game controllers and the like, you might consider singleton patterns that can automatically initialize the controller the first time it is referenced.
Thanks for the reply.
I'm new to unity dev, so to get an understanding of "whats bad" and "whats good".
How bad would it be performance wise to have the empty gameobject described above with ~200 scripts attached and each script contain an Update()?
I read somewhere that limiting the number of scripts containing Update() is a good practice.
Welcome!
That question is getting more subjective, I think.
If I attached 200 scripts to one object, I'd be inclined to combine some of those scripts and/or break that into multiple GameObjects -- if nothing else, 200 components in the inspector is going to be overwhel$$anonymous$$g for designers.
Unity does come with a built-in profiler so you can check how much CPU time your scripts are using. I've had scenes with thousands of scripts running, so it really depends on what they're all doing. The more stuff you have going on in the scene, the more you may need to optimize for performance. General advice is to avoid worrying about that until it becomes a problem.
Answer by tanoshimi · Nov 06, 2015 at 06:09 PM
It's generally good practice (not just in Unity) to place all code logic as close as possible to the thing to which it relates. So character controllers should be on characters, enemy AI should be on enemies etc. If you have generic global game controllers, make a generic game controller object (I.e. The empty Gameobject you describe) and attach them to that. It's perfectly good, and very common practice.