Callbacks In C# And Unity

Ben Pielstick
Jun 21, 2021

--

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.

--

--

Ben Pielstick

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