Introduction to Asynchronous Programming in Unity |

Dive deeper into the world of asynchronous programming in Unity by creating a fun town-builder game.

This is a companion discussion topic for the original entry at

Nice work, I’ve not come across async in Unity before and this was a good set of explanations.

I do have a couple of points regarding the cancellations.

1/ I didn’t get any exceptions when cancelling tasks but the try…catch into the console worked.
2/ Cancelling a task doesn’t rollback any work done by the task (as it stands). There is a tile instantiated to mark construction, so you would expect this to be removed; however the task simply stops and doesn’t delete/release any resources it created. It could be deleted from the hierarchy by the synch processing, but there could be several constructions going on - which one would it be? Is there a more advanced mechanism to handle this scenario or would we need some sort of garbage collector to be triggered if a cancellation happened?

Hey there, thanks for following along and glad to hear that this got you learning something new in Unity.

Not sure why the cancellation exception wasn’t coming up for you, do you know if that was happening when you were cancelling a task by pressing Escape or when exiting play mode? Regardless, good to hear the try...catch block worked out for you.

That is correct, just cancelling the task means to not process it further and wouldn’t revert any of the previously ran steps. So, if you wanted to do some cleanup, you would have to set something up on your own.

One way to do this could be to store the spawned items in new collection for each structure being built. Then in the catch block, iterate through the collection and destroy any object spawned. Of course, the specific steps you do here would be highly dependent on the work you are doing and I felt that adding in this behavior was a bit out of scope for the article; but it is a good point to bring up.

Thanks for stopping by!

I reviewed this tutorial and I have a question.
In the code that kicks off the async you don’t seem to be calling it in an async manner. Specifically:

private void BeginBuildingStructure()
  constructionManager.BuildStructure(currentPlacementPrefab, GetGridPositionOfMouse());

This is a non async body of code calling an async function. I realize that your async func has void return type, but I was hoping to learn how you glue unity and async together. For example a user clicks on a menu button and it does a network call and handles the result. Thats a unity event call, hitting an async function, waiting for the call and then handling the result.

From what I have read async void is not a best practice and it seems like its being used in a fire and forget manner here instead of “async from top to bottom” as seems to be the preferred way.

I took a random guess that adding the word async to a button handler might just magically work and it did but I wasn’t sure if this is really the right way to do it or not. Any ideas?

Great tutorial by the way!