I am a newbie to Unity (but with game design experience) certainly asking simple questions....
I understand that scripts attached to gameobjects are called every loop of the game and the gameobject script is written to control that instance of a gameobjects actions within the game.
I am, for my first go at a game in Unity, planning a arcade style game where as the game moves along new enemies appear (with stronger enemies over time).
This needs a "master script" that is controlling the flow of the game. Among other things this master script would keep track of what enemies are being displayed now and as enemies are destroyed creating and placing the next new enemies.
How would this master script be implemented so it is also called every frame of the game?
I could call the master script from the player gameobject script and this would work.
But I think perhaps unity has some support for a master script that is not directly tied to any gameobject but is called every frame to provide control of the game.
Is this so? How is this implemented? If it is so I expect there is some documentation (which I am hapazardly reading so forgive me if its someplace obvious!).
asked Aug 17 '10 at 02:23 PM
Unity3D does not support running MonoBehavior scripts that are independent of gameobjects. (MonoBehavior scripts are the ones that get Update, FixedUpdate, and related function calls from the engine.)
Your initial thought was correct: You put your master control script onto a gameobject that will persist for the entire level. So the player gameobject is a decent place to put it. I tend to put my master scripts on a gameobject called "GameManager" so that their location is more obvious in the hierarchy.
[Updated to take into account Worlfram's comments]
The best approach to a Master Script is an empty GameObject with the property DontDestroyOnLoad enabled. Attach your master script to this game object.
Note that loading/reloading a scene in Unity resets all GameObjects, their values and their scripts (except those with DontDestroyOnLoad enabled). So the Master Script is the proper place to control the global game flow and variables (i.e. levels, score, life...). The level-specific game flow (i.e. enemies on that level) could be controlled from an script in the player's gameobject, as it will be restarted on loading/reloading the level).
answered Aug 18 '10 at 12:33 PM
Any script that's attached to a game object will have its Update() method called every frame.
You can simply attach your master script to an empty GameObject for that purpose. There are several methods of finding/referencing "real" GameObjects or other scripts from that script, to be able to modify these objects, or call functions in their scripts (using Tags (e.g., to find all enemy ships), or creating public var's to make them show up in the Inspector and then dragging the other GameOBject onto them, or the GameObject.Find() functions, or the SendMessage() methods, etc.)
answered Aug 17 '10 at 03:45 PM
You can also use C# static classes to store some information or functions withouth them being in a gameobject.
answered Aug 17 '10 at 03:44 PM