Call functions in other scripts like Update() and Start()

Hey everybody!

I’m very curious about how to do the same as Unity does with Update and Start methods. Like having a script which handles the function calling and call the method for each script that has this method implemented in the class.
The only way I know how to do this is using events and delegates, but this way I need to subscribe the method to the event(or delegate, just forgot), and I want something more ‘automatic’, like:

public class MethodHandler: MonoBehaviour {

void CallSometime();

void Update () {

if(blabla == blabla1) {

CallSometime();

}

}

}

public class MethodImplementation: MonoBehaviour {

void CallSometime () {

print("I've been called!");

}

}

Note that ‘MethodHandler’ doesn’t know about the existance of ‘MethodImplementation’, howerer, CallSometime() is called in ‘MethodImplementation’ when it’s called in ‘MethodHandler’, just like the Update() and Start() methods. Is this possible?

Thanks from now!

I’m not sure if there’s a built-in way to have Unity send a message to ALL scripts in the scene. However, you can easily have a script on one object call a method on every other script attached to that object, using SendMessage (Unity - Scripting API: GameObject.SendMessage).

For your example above, as long as MethodHandler and MethodImplementation are both attached to the same object, SendMessage will accomplish your goal.

I suppose if you really wanted to send to all scripts, you could walk the scene graph and call SendMessage on every object. But this approach would probably not be very performant.

Edit: Looks like there is also a “BroadcastMessage” method that you could call on the root of your scene: Unity - Scripting API: Component.BroadcastMessage

What do you want is a BroadCast for all objects in scene. You can´t do it. Try to search “broadcast all”, you may found some solution for your problem.