Callbacks In C# And Unity

When you’re starting out with Unity, one of the things you’re almost certain to run into is race conditions. When you start up your game or load a new scene and everything starts up at once, you may have a hard time making sure things happen in a specific order. Callbacks are a great solution to this, assuming you’re already familiar with using delegates.

Set somethingIsDone as a callback method, which is passed as a parameter to doSomething, so that it can be called whenever the logic of doSomething is finished

As you can see, this is a very convenient way to enforce an order of operations, especially across multiple GameObjects which are all doing things at the same time. There are some even more advanced ways to streamline this code such as Actions and Lambda expressions, but these aren’t really necessary in order to start taking advantage of callbacks.

--

--

Game developer with over 12 years of experience, mainly focused on MMOs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ben Pielstick

Game developer with over 12 years of experience, mainly focused on MMOs.