Unity XR - How to catch bugs

Date: 2023-01-20T06:33:18+00:00 - (Skid Vis)

Debugging VR applications presents unique challenges that traditional debugging methods don’t adequately address. In this comprehensive guide, I’ll show you how to effectively track and manage bugs in your Unity XR applications, especially when they’re running on a headset where you can’t see the console logs.

The Problem with Traditional Debugging in VR

When developing standard Unity applications, Debug.Log is often our go-to tool for troubleshooting issues. We can see messages appear in the console, track variables, and identify where problems occur. However, this approach falls short in VR development for several reasons:

  1. You can’t see the console while wearing a VR headset - This makes real-time debugging difficult
  2. Console logs aren’t available in production builds - Once your app is deployed, you lose access to these logs
  3. User-reported bugs are hard to reproduce - Without detailed error information, it’s challenging to understand what went wrong for your users

To demonstrate this limitation, I’ve set up a simple scene in Unity 2021 LTS with:

In a traditional approach, we might use code like this to detect interactions:

private void OnTriggerEnter(Collider other)
{
    Debug.Log("You have entered the trigger");
}

While this works fine during development when you can see the Unity console, it’s useless once your application is deployed to users or when you’re testing with a headset on.

Introducing Bugsnag: Remote Error Tracking for VR

To solve this problem, we can use Bugsnag, a powerful error monitoring platform that supports Unity applications. Bugsnag allows you to:

Best of all, Bugsnag offers a free tier that’s sufficient for most indie developers and small projects.

Setting Up Bugsnag in Your Unity XR Project

Let’s walk through the process of integrating Bugsnag into your Unity VR application:

Step 1: Create a Bugsnag Account

  1. Visit bugsnag.com and sign up for a free account
  2. Once logged in, create a new project
  3. Select “Unity” as your platform
  4. Give your project a name and click “Continue”

Step 2: Install the Bugsnag Package in Unity

Bugsnag provides a Unity package that you can install directly via the Package Manager:

  1. In Unity, open the Package Manager (Window > Package Manager)
  2. Click the ”+” button in the top-left corner
  3. Select “Add package from git URL
”
  4. Paste the Bugsnag Unity package URL provided in your Bugsnag dashboard
  5. Click “Add”

Unity will download and install the Bugsnag package from GitHub.

Step 3: Configure Bugsnag in Unity

After installation, you need to configure Bugsnag with your API key:

  1. In Unity, go to Window > Bugsnag Settings
  2. Copy your API key from the Bugsnag dashboard
  3. Paste it into the API Key field in the Bugsnag Settings window
  4. Save your settings

Step 4: Replace Debug.Log with Bugsnag.Notify

Now you can update your code to use Bugsnag instead of Debug.Log:

private void OnTriggerEnter(Collider other)
{
    // Instead of Debug.Log
    Bugsnag.Notify("triggered", "You have entered a trigger", "nothing here");
}

The Bugsnag.Notify method takes three parameters:

  1. Error name or type
  2. Error message
  3. Stack trace (optional, you can leave this empty or add custom context)

Step 5: Handling Exceptions with Try-Catch

Bugsnag really shines when handling exceptions. You can wrap potentially problematic code in try-catch blocks and report any exceptions to Bugsnag:

try
{
    // Your code that might throw an exception
    // For example, processing an in-app purchase or loading external data
}
catch (Exception e)
{
    // Report the exception to Bugsnag
    Bugsnag.Notify(e, "error", "On trigger for the Box");
    
    // You can also specify severity
    // Bugsnag.Notify(e, Bugsnag.Severity.Error, "On trigger for the Box");
}

Real-World Use Cases

Testing with Friends and Beta Users

When you distribute your VR application to friends or beta testers, Bugsnag allows you to:

Monitoring Production Applications

For released applications, Bugsnag helps you:

Tracking Custom Events

Beyond error reporting, you can use Bugsnag to track important events in your application:

Viewing and Managing Errors

Once your application is sending data to Bugsnag, you can view all reported issues in your Bugsnag dashboard:

  1. Each error appears in your project’s inbox
  2. You can see when the error occurred
  3. Detailed information about the device, OS, and application state is provided
  4. You can assign, comment on, and track the status of each issue

Conclusion

While Debug.Log is a valuable tool during development, it falls short when debugging VR applications in production environments. By implementing Bugsnag in your Unity XR projects, you gain powerful remote error tracking capabilities that help you identify, understand, and fix issues more efficiently.

This approach is especially valuable for VR development where traditional debugging methods are limited by the nature of the platform. With Bugsnag, you can continue to monitor and improve your application even after it’s in users’ hands.

For VR development, I use the VR Interaction Framework (VRIF), which provides a solid foundation for building interactive VR experiences.

If you’d like to discuss VR development further, feel free to join my Discord server where we can chat about various aspects of XR development.

For those who want to support my work, consider becoming a Patreon member.

The music in my video is by Flawed Human Being, whose work you can find on Spotify.