Al Heck

It’s a specialized delegate that all gameobjects listen to. That way, instead of assigning via a for loop, I can call an event and any gameobject that is set up to listen to that particular event will react appropriately. Let’s create a button that turns all cubes red.

Let’s setup our event, first declaring a delgate and then a static event called onClick. We will then apply it to our TurnRed() function.

Now a new C# script for the objects. The script, which can be placed on any object, will now grab the material and run a method — in this case, it will turn the gameobject red.

Why is this useful? When you create your event, you are assigning it to anything — a mouse click, a button click, a trigger. Whenever this THING happens, it runs a function. Usually you need to do a getcomponent to actually see this happen. In this instance, we have the scripts register the script at the start and then wait until the event occurs. When it does, it runs the function.

Unity Developer and Software Engineer who loves making games with GameDevHQ