- Home /
Newbie... Master script?
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!).
Answer by SteveFSP · Aug 17, 2010 at 03:27 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]
That is not entirely correct, you could use the ScriptableObject class to create scripts that actually are independent. (which also means they won't have an Update() method, etc.)
...I forgot to add: "However, you rarely need them, if ever. I never had to use ScriptableObjects - simply adding "normal" scripts to empty GameObjects works quite nicely.
Answer by Edy · Aug 18, 2010 at 12:33 PM
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).
Answer by Wolfram · Aug 17, 2010 at 03:45 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.)
Answer by Veli · Aug 17, 2010 at 03:44 PM
You can also use C# static classes to store some information or functions withouth them being in a gameobject.
Answer by Billu · Apr 01, 2016 at 04:05 AM
Usually you can attach the 'master script' on the camera. As long as the game continues, the camera are assumed on. Of course if your game switch cameras frequently this would not work
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
How do i make an animation play on key press? 3 Answers
Scripting error #2! 2 Answers
get all scripts attached to gameobject 2 Answers
FPSMouseScript.js 1 Answer