Unity: Debugging Infinite Loops

Unity’s Debug.Log has a singularly frustrating interaction with infinite loops: it stops working immediately upon entering the loop. My theory is that Debug.Log is designed only to output to the console at the end of a frame, so if the end of the frame is never reached messages never get written. Whatever the cause, this fundamental debugging tool is useless for working out what code is involved in the loop.

Fortunately, there’s an easy solution: write to a text file. Debug.Log messages won’t show during an infinite loop, but Unity will honor instructions to write to a file as it goes through code endlessly. By writing messages to a file instead of to the console, you can get the information you need.

Outputting text to a file is easy. Copy the following into a new script:

using UnityEngine;
using System.Collections;
using System.IO;

public class FileIO {

/// <summary>
/// Write a string to a file
/// </summary>
/// <param name=”fileName”>File to write to.</param>
/// <param name=”content”>String to write.</param>
/// <param name=”append”>If set to <c>true</c>, append. If set to <c>false<c>, overwrite file.</param>

public static void WriteStringToFile(string fileName, string content, bool append)
{

StreamWriter sw = new StreamWriter(fileName, append);

sw.WriteLine(content);

sw.Close();

}

}

You can then use it as follows:

FileIO.WriteStringToFile(“debug.txt”, “Animation begins”, true);

Write as many messages as necessary to determine what’s happening.

A few usage notes:

  • Don’t forget “using System.IO;”
  • Note that this class doesn’t inherit from Monobehaviour, so it can’t be attached as a component. That’s fine; use the syntax above to call the function.
  • By default, the text file appears in the Assets folder.

Here’s hoping this saves someone out there a little time and heartache. 🙂

Unity: Static Batching Timing

An interesting, undocumented aspect of static batching in Unity: static batching occurs between Awake and Start. Objects in the hierarchy at that point will batch; anything created later will not.

This is true even when loading scenes additively. If as the additional scene is loaded in Awake(), and the additional scene contains a script instantiating objects in its Awake(), those objects will undergo static batching if they’re otherwise eligible. If the scene is loaded in Start(), or the instantiation occurs after Awake(), they won’t. There are lots of weird timing issues with additive scene management, but this, at least, seems to work as expected.

I hope that this saves someone out there the hour or so it took us to figure out when we needed to be instantiating environmental objects. 😉

Unity: Shaders and Static Batching

I’ve learned a lot about static batching in Unity today–not least that it has more requirements than appear in the documentation. 😉 Does anyone out there know of good resources on how batching and shaders work (or don’t work) together? Gameobjects in a project I’m working on aren’t batching, and it’s definitely because of the shader.

So You Want to Try Game Design

Welcome! Game design is an endlessly fascinating and exciting field. It’s awesome that you want to try it out, and you can get involved without needing to invest anything more than your time and curiosity.

A lot of people will tell you start by learning something like Unity or GameMaker. Those are both good programs, but there’s a certain amount of lead time involved as you learn them. My suggestion would be to start by creating some games; once you’ve gotten a sense for where you want to go with your first design(s), you’ll be better positioned to judge which tools will help you get there.

Instead of working on the computer, begin by learning with Magic: the Gathering. Try making up some cards, and then play them and see how they work out. (Don’t worry about making them pretty–just cut up slips of paper and sleeve them with normal cards.) Expand that into a full set, or design a cube for drafting. Get used to reaching into a game’s systems.

While you’re doing that, play some games that will give you a sense for just how vast the genre is, and how limitless the opportunities for creativity are. Gone Home, Proteus, Beyond EyesPapers, Please–the list goes on. Challenge yourself to see games in new ways.

Don’t neglect to read, either. There’s a lot of great writing about game design out there. The links page above has some; when you’ve exhausted that, I’d recommend Salen and Zimmerman’s Rules of Play.

Game design is sometimes fun, always challenging, and incredibly rewarding. I hope you enjoy it as much as I do, and that the advice above helps you get started. Again, welcome!

Unity UI: Backgrounds

A question for the group: how do you handle placing UI images against a background in Unity? I’m looking to create a title screen with a consistent look across different screen resolutions and aspect ratios; my goal is that UI elements will always appear in the same place relative to a background image that provides frames for them. At the moment the UI elements move around more than I’d like when viewed on different screens. Any tips or tricks?

Inquiring Minds Want to Know

OK, Unity question:

What is the actual mathematical relationship between the scale of a parent object and the local position of a child object? It doesn’t seem to be 1-1; if the scale of the parent increases by 10%, the child object’s local position doesn’t reliably increase 10%. Anybody know what the equation is?

Inquiring Minds Want to Know: Compilers

(Again, I’m out of town and this is being posted automatically; please forgive delayed responses!)

In the spirit of choosing the right tool for the job . . . .

For a while now I’ve used MonoDevelop for C#/Unity work, and Microsoft’s Visual Studio 2013 for C++. I’ll be frank in saying that I chose both more for their visibility than because of any specific feature they included; MonoDevelop is literally packaged with Unity, and Visual Studio is easily found when one goes looking for C++ compilers.

As time has gone on I’ve become curious about alternatives. My searches have led me to lots of options–but, interestingly, not much guidance as to how to choose between them. Various people online suggest using one compiler or another, but they rarely explain why one should prefer this over that..

Coders, what factors go into your choice of compiler? Do you have specific recommendations for the languages above, or for other languages?