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. 🙂

Advertisement

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?

Oh, Debugging

I know there are at least a few people with programming experience who read this blog, so I thought I would run something past them.

Earlier I ran into a problem while scripting in C#–the code compiled, but it didn’t produce the in-game effect I expected. (Specifically, an OnTriggerEnter2D method didn’t trigger, even though the code and all in-editor settings appeared to be correct.) I tracked the problem down to a single line, but couldn’t find any issues with that line. Finally, lacking any better ideas, I deleted the line and retyped it, character for character. Just like that, the problem was resolved.

Two questions:

1. What might have caused this issue? I suppose the most likely answer is “there was a typo in the line after all,” and that’s certainly possible–I had been staring at the code for a long time when I finally narrowed the problem down. Are there other possibilities?

2. Normally I want to treat situations where a problem came up and had to be solved as learning experiences. If we never figure out specifically what happened here, what lesson do you think I should take away?

Something Completely Different: Ideas for Games Involving Drones?

When I run into a roadblock while coding Lines of Questioning, I sometimes mess around with other tools and ideas as a way to clear my mind. Recently that’s meant experimenting with the Flying Drone Toolkit. (Full disclosure: I know the Toolkit’s creator.) It’s basically a pre-programmed setup for implementing VTOL drones in 3D games.

To date I’ve just been poking around and learning about the Toolkit’s features. That’s fine so far as it goes, but there’s a natural temptation during that sort of exploration to focus on what the Toolkit does easily rather than on what it can do with some effort. I need a project to really test its–and my–boundaries.

In searching for ideas I’ve tried to draw inspiration from the way the drones fly. They don’t act like jet planes; rather, they move like helicopters. What else flies that way, and what interesting things to they do?

– A news helicopter that has to fly around getting good footage.

– A guardian angel that has to protect someone without being seen doing it.

– Superman, being Superman.

– Vehicles moving in zero gravity, that have to pick out precise orbits or land on broken extraterrestrial terrain.

– Vehicles that move like they were in zero gravity–martian landers, repulsorlift speeders in Star Wars, the U.S.S. Voyager when it lands–carrying people to and from adventures, often through hostile terrain.

I like some of these ideas (let’s be honest, I basically like any idea involving Superman), but I feel like I’ve only scratched the surface. If you could make any game involving something that flies like a VTOL drone, what would you do?