x


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!).

more ▼

asked Aug 17 '10 at 02:23 PM

RSud gravatar image

RSud
196 25 27 36

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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]

more ▼

answered Aug 17 '10 at 03:27 PM

SteveFSP gravatar image

SteveFSP
1k 8 13 29

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.)

Aug 17 '10 at 03:50 PM Wolfram

...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.

Aug 17 '10 at 03:51 PM Wolfram
(comments are locked)
10|3000 characters needed characters left

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).

more ▼

answered Aug 18 '10 at 12:33 PM

Edy gravatar image

Edy
637 2 5 17

(comments are locked)
10|3000 characters needed characters left

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.)

more ▼

answered Aug 17 '10 at 03:45 PM

Wolfram gravatar image

Wolfram
9k 8 20 52

(comments are locked)
10|3000 characters needed characters left

You can also use C# static classes to store some information or functions withouth them being in a gameobject.

more ▼

answered Aug 17 '10 at 03:44 PM

Veli gravatar image

Veli
61 3

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3328
x798
x224
x16

asked: Aug 17 '10 at 02:23 PM

Seen: 2094 times

Last Updated: Aug 17 '10 at 02:23 PM