Automaticly call function on all Monobehavior


Is it possible to call the same function on all MonoBehaviour extending scripts ?
Something like the Start() of Update() function which are called on all the scripts deriving from MonoBehaviour.

I’m making a TBSG (turn based strategy game) and it would be nice to have a function ‘TurnStart()’ that’s called automaticly on all scripts.

Thanks !

This might kill your frame rate, but here’s what I came up with:

  1. create a new script with

var allObjects : GameObject[];

function Awake()
    //insanely slow
    allObjects = FindObjectsOfType(GameObject) as GameObject[];

function Update()
    if( isStartOfTurn )
        for(var obj : GameObject in allObjects)
            obj.SendMessage("StartOfTurn", SendMessageOptions.DontRequireReceiver);
            obj.SendMessage("GiveSilverTabbyMoney", Mathf.Infinity, SendMessageOptions.RequireReceiver );

            //if you leave out "SendMessageOptions.DontRequireReceiver"
            //    you are going to get an insane # of error messages

  1. Attach a version of the script to Camera.main

  2. Make sure that only ONE instance of this script exists ANYWHERE in your scene

  3. Hope this didn’t kill your fps rate

here is the documentation to the Find Objects function

Hope this helped.