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?

Advertisement

4 thoughts on “Oh, Debugging

  1. There are a variety of character alternates that can botch code. For example, ‘ and ’ or the space associated with ALT-32 versus the space associated with ALT-255.

    Also, sometimes compilers and coding software get confused, just as any program can.

    My takeaway. Everything is fallible all the time.

  2. Additionally, it’s possible that somehow Unity (I assume you’re using Unity) had not compiled the latest version of the file. Retyping the line and saving the file would trigger a re-import, which could have fixed your problem.

  3. At first I was thinking what Jay Treat said, that there could have been a bad character in that line. But after some consideration, I actually think that isn’t that likely since the code actually compiled. If you add a random (say, invisible) character to code like that it is more likely it won’t compile. The main exception would be if there was some string(s) involved (say as parameters to the function).

    Which leaves a coincidental re-compile (Jon Gills) explanation a bit more likely, or your own suggestion that you had just typed it wrong.

    If you still have the editor open you could backup the entire file, and then keep hitting undo until you get back to the previous state before you retyped the line. You can probably narrow down the exact problem to prevent it from happening again. If you can get into the ‘bad state’ again, you can even use a hex editor to see if there is a weird character, or a diff program to compare the before and after (at a binary level).

    Even if you can’t repro the problem, if you cut and paste the entire line you retyped I can try to give better ideas.

    Another possibility is the issue wasn’t actually fixed, but for some reason the state of the device/PC was different, so it just happened to work that time. The bug could return again in minutes, or days later. These are the hardest to fix (:

    I don’t know C# of that specific method, but it looks like some sort of callback. I’ve seen many cases where callbacks aren’t called, which can be everything from misuse of the APIs to set up them, to threading issues, timing issues, or even (possibly) bugs in the framework itself – Unity in this case.

    In the future, if you get a weird problem like this, you can try first restarting the dev environment (IDE) and then even your computer. It sounds old fashioned, but sometimes it actually works.

  4. Thanks all for your thoughts. The problem persisted over two days (with an editor restart and a computer restart in the middle) and through several saves of the script, but perhaps it didn’t re-compile during that time, either due to a bug or because none of the changes I made as I was looking for the problem prompted one.

    I didn’t consider the different-characters-that-look-similar issue. Using a hex editor to look at the characters in detail is a great idea that I’ll definitely keep in mind for the future. I can’t undo back to the non-working version at this point (and I didn’t save the old, non-working version–hrm, need better version control), but I can see why that would be valuable as well.

    Thanks again–it’s great to be able to bounce these issues off of you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s