Unity Respawn using Delegates and Events
Here’s a quick way to respawn in Unity using delegates and events…
In this example, we’re going to use the SpaceBar to initiate the respawn event:
Inside our Main script (attached to the MainCamera), we’ll create the Teleport delegate and give it a Vector 3 value that will be the destination. And also create an event that is tied to the delegate, and make a variable to hold our destination as a Vector 3 value:
Add the space bar as the trigger:
Inside the Cube script (attached to the cubes) we’ll add the Respawn method and attach it to the onTeleport event:
Now the Respawn method will be invoked from the Teleport delegate when the onTeleport event is triggered from the space bar.