- Home /
"Main" function?
I'm new to Unity! (though I'm getting the hang of it pretty fast!)
Anyway, in the past I have scripted many games in other languages and platforms, and I'm sure Unity is no exception here: Every game basically has a "Main" loop, where everything happens: Updating all the game objects, processing data like input etc...
I am wondering, where might I access the equivalent to such "Main" function in Unity? I am assuming each scene has its own Main or Update function, but I'm having trouble finding it.
I wish to do a few things when a scene loads.
Well, that's not a big problem for my project right now, because I could simply create a new GameObject and put those things in the Awake() function or something, and ta-da. But I was wondering if there is a more "official" way to do things on scene initialization.
Answer by Eric5h5 · Nov 21, 2010 at 07:18 PM
There is no Main function in Unity; everything is component-based. Your solution (create an empty game object with a manager script attached) is the usual way of handling scene-wide initialization.
It may seem odd at first, but after using Unity for a couple of years or so, the concept of a $$anonymous$$ain function seems backwards and limiting now.
After using Unity for a couple of days or so, the lack of a $$anonymous$$ain function is a bit desperate. But I'll get used to it =D
Answer by BurningThumb · Nov 21, 2010 at 09:37 PM
Having a Main() function would defeat the purpose of having an engine.
The engine is started by main and form there it calls the functions here when they are required:
http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.html
If you are familiar with object oriented programming(which is in the best interest of all developers) understand that that for every script you make you also create a class by its name. When the engine calls update it calls MonoBehaviour.Update() in everyone of your classes.
Your answer
Follow this Question
Related Questions
Creating a scene using script (which Start() to use) 2 Answers
How do i initialize only once for a scene? 1 Answer
Declare array in other function = error 1 Answer
Start menu gui wont load scene 2 Answers