Am sure this obvious to everyone but me!

We are slowly coming to understand the script behavior and accessibility in unity, which is great, but something we are lacking is an understanding on how/when things are run.

For instance, we understand the order in which scripts attached to objects are called… Awake(), Start() etc…

BUT… in what order is each objects scripts called? In order of object in the heirachy?

Is there a master script interface that doesn’t get attached to a game object, but is persistent? like a master game loop that we can code for/in?

Is there a Start() function for the entire game? so we can set up things we need? And an End() for the entire game?

For instance, if we want to have some global variables that set the current level, score, time etc… that are not specific to ANY game object.

Thanks!

You can make an empty and attach a “master control” script to that. Actually you can attach it to any object that’s permanent (objects can have any number of scripts attached to them), but it’s nice from an organizational point of view just to use an empty.

–Eric

The order of call between objects is undefined and you should not make any asumptions on the order. If you need to maintain an order of execution between objects, call methods (or use SendMessage or BroadcastMesage) from the first to the subsequent instead.

For simple dependencies, you can rely on that all loaded object’s Awake methods have been invoked before any Start methods are called. Similarly you can use LateUpdate to ensure that an operation comes after an Update on a different object.

The basic rule we use at Serious Games is to use Awake for initializing ourselves, and Start for speaking with other objects…

Let us take the example of a manager class:

class NPCManager : MonoBehaviour {
    public static NPCManager get;

    void Awake () { get = this; }

    public Vector3[] GetAllNPCPositions () { /* blah blah blah */ }
}

We simply throw this behaviour on ONE GameObject in the scene - now any other scripts can do NPCManager.get to get the instance of the NPCManager, then call GetAllNPCPositions.

class BlobShadowThingy : MonoBehaviour {
    // All these are valid
    void Start () {
        Vector3[] positions = NPCManager.get.GetAllNPCPositions();
    }

    void Update () {
        Vector3[] positions = NPCManager.get.GetAllNPCPositions();
    }

    void LateUpdate () {
        Vector3[] positions = NPCManager.get.GetAllNPCPositions();
    }

    // THIS ONE IS NOT: 
    void Awake () {
        // We don't know which awake is called first.
        Vector3[] positions = NPCManager.get.GetAllNPCPositions();
    }

}

BUT, they cannot do that in Awake (as we don’t know if NPCManager.get has been set yet), all other methods are ok.

Thanks everyone for your responses :slight_smile:

I am using the Awake() on an empty gameobject to do all initializing like a “master control script”, like Eric5h5 suggested. I was just hoping there was a more official way of dealing with this.

Freyr… thanks for the clarity on object calling order. I understand how this cant really be relied on, though thought there might be a way of Unity notifying me at runtime the “next” object it will be checking.

Nicholas… hmm, your example is beyond me… I am familiar with the .NET framework when working in ASP.NET but I am still at a loss as to how to be using mono correctly with unity. I understand the principles but… well, i wish the were more in-depth tutes for unity for the scripting areas.

Funnily, we have only been playing with Unity for 2 days now… and I am impatient in my learning as always. It seems like a very powerful engine… but the docs seem a little vague in many areas.

The forum has been a great help so far… readying posts, and also getting replies to my own posts.

Thanks again for all of your suggestions.

I guess you are referring to the use of C# in Nicholas’es examples.

I took the liberty of translating them to JavaScript:

// Filename: NPCManager.js
static var get : NPCManager; 

function Awake () { get = this; } 

function GetAllNPCPositions () { /* blah blah blah */ }
// Filename: BlobShadowThingy.js

// All these are valid 
function Start () { 
    var positions = NPCManager.get.GetAllNPCPositions(); 
} 

function Update () { 
    var positions = NPCManager.get.GetAllNPCPositions(); 
} 

function LateUpdate () { 
    var positions = NPCManager.get.GetAllNPCPositions(); 
} 

// THIS ONE IS NOT: 
function Awake () { 
   // We don't know which awake is called first. 
   // (So NPCManager.get might not have a value assigned yet)
   var positions = NPCManager.get.GetAllNPCPositions(); 
}

Btw. you are in fact already using the .Net framework when scripting Unity even when using JavaScript, as all scripting is done through Mono.

Thanks Freyr,

I understood the C# code… that wasn’t the problem… its understanding the concept behind it. I cant seem to find what the NPCmanager class type is in the docs.

Clearly the concept behind the script is to show that you can poll a bunch of stuff (in this case positions of things) through this function form any other Start, Update etc… function from any other gameobject, so all objects can know about positions of each other… is that correct?

To be honest, abstract examples leave me clueless most of the time. I learn better from actual code examples where I can work out exactly what it is doing. (that wasn’t a request for actual code examples) :slight_smile:

This will just take some time to get comfortable with, but we are determined to master Unity3D, so you will be hearing a lot from me, and others here at Sector3 during our learning process.

By the by, just a bit of info about us, if anyone is interested… We are primarily an 3D animation and VFX studio making TV commercials, TV shows and Music Video clips… that has also done a tonne of multimedia games in the past, mainly in flash and director, and 95% of the time for PC only.

I bought a new MacBook Pro (my first mac) and am such an Apple fanatic now that I have since bought some iMacs for the office and have decided that the PC world can go (insert vulgar expletive here) and that I want to make cool stuff for the Mac community.

Well, I am sure I have bored you all enough… thanks again for all of your help!

welcome seon, you’ll find unity a great place to be.
i checked out your site, really nice work btw ; )

Thanks DRJones…

We hope to be able to deliver the same standard of creativity to our game concepts too!

And yes, we have found the Unity community very welcoming and informative!

Because it’s a custom class written by the guys at Serious Games for their own content, thus you won’t find it in the docs.

The example Nicholas was citing is a case in which he’s trying to demonstrate/show that you cannot guarantee the order of Awake() calls on GameObjects, thus and cross-GO serialization should occur after Awake() (like Start() for example).

HiggyB: Well, that clears that up! And I thought it was just the docs :wink:

As Tom said it is a custom class and therefore not in the docs. The NPCManager is the class defined by the first code snippet in my (and Nich’s) post. When you create a JavaScript file in Unity, you are also defining a class with the same name as the file the script is stored in (minus the .js ending.) That’s why it says it stored in a file called NPCManager.js

Seon,
There is a good example of a manager class script (due to Freyr) on the Unify Wiki site:

http://www.unifycommunity.com/wiki/index.php?title=AManagerClass

Richard.