I’m new to Unity3D but I’m a long time .NET developer, primarily using C#. I have tried a couple of engines/libs out there like XNA, Irrlicht, and most recently, UDK. But so far Unity has been easy to work with.
Enough intro… my question is where do I instantiate objects globally? I mean in normal C# apps, you have your main method/main class where you can instantiate your game managers and use them within your game/app. I’m well aware that Unity attaches scripts to components. So far, the only way that I see to achieve such a behavior is to attach a script to the main camera on your initial level and instantiate your game managers within that main camera script.
Is that how you guys do it? I want to know the “best practice” for doing this in Unity.
Welcome Roundhouse!
You can create an empty gameObject and attach a script which manage all managers, or using something that you know will persist along the game… like your character .
The first case usually it is considered… mmm… neat? because it is independent (i mean, you can add or remove objects, without worrying if one of those has or not the manager’s initializer). However i prefer attach to the main character the script to be responsible of execute managers; it is very easy to identify and you are sure that it will be “alive” through the game.
greets
EDIT: =========================
I changed components by objects to avoid misunderstandings.
Yeah, I like your first suggestion. Having an empty gameObject makes sense. I have read a number of tutorials/blog entries about GameManagers, AudioManagers, etc. and was really wondering where do they instantiate these managers. Thanks for the info man!
Yeah, I did the same thing for my application at work. I have a single “Bootstrapper” script which is attached to an empty game object. The Bootstrapper then initializes my core “game/manager” class (essentially the “main” method) which does everything else in the game.
I only have a few other Unity-scripts in the game, most for listening and firing events (e.g., OnUpdate, keyboard, mouse) so most of my game is really independent from Unity’s whole MonoBehaviour/scripting paradigm.