This site uses strictly necessary cookies. More Information
X- 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
Welcome to Unity Answers
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Follow this Question
Related Questions
Creating a scene using script (which Start() to use) 2 Answers
Initialising List array for use in a custom Editor 1 Answer
OnSceneExit? 3 Answers
Networking, Scene dependent? 1 Answer
Call a fuction only once on startup 1 Answer