Minimizing string GC in Unity

Posted on

An easy trap to fall into when you're starting out in Unity is creating strings each frame in your update loop.  Say you have score that can change from a bunch of things: eliminating foes, jumping on a block, collecting a ring, or just existing. Whatever floats your boat.  Then in your score updater component you just do: void Update(){ textComponent.text = "Score: " + score.ToString(); }Easy and done.  Problem is, you're now creating a string (which are immutable in C# and allocate memory) each frame.  This will really add up and create lots of garbage which will need collecting.The U…

Read more »

Actions and Selections with Card Lang

When the events we set up in part 1 of exploring the card language fire it would be quite nice if they could actually do something! This is where actions get their chance to shine. There are a ton of different actions a card can take but we'll start out with just a few example ones like doing damage to pieces (Hit), healing them, or drawing a card. Just as we can have multiple events on a card we should also be able to have multiple actions for each event. We'll also need a way to pass arguments to these actions to tell them which pieces to hit, what player draws the card, and so on. So witho…

Read more »