Unity: Use Foreach with Debug.log to Debug a List

by Michael Sacco Published January 13, 2024
The cover for Unity: Use Foreach with Debug.log to Debug a List

Introduction

You need to be capable of quickly debugging a list while developing your game. To do so, you will use a combination of the foreach statement and the Debug.Log method. This guide will teach you how.

What is a list in C#?

A list is a collection. A list represents a number of ordered values. A list allows the same value to occur more than once. Lists are one-dimensional.

There are many types of collections in C#. Here are some examples of other collections:

📄 Resources:

Foreach in C#

foreach is a statement that iterates over each element of a collection. The foreach statement consists of two parts: the statement and the body. The statement describes the set to iterate. The body describes what to execute for each item in the set.

In pseudocode:

foreach item in list do something

Foreach with Debug.Log

Basic foreach Example

The foreach loop is a fundamental construct for iterating over collections in C#. In the context of Unity, this loop makes it easy to traverse a set of elements contained in a list.

Logging the progression of the iteration provides real-time insights into the loop’s execution.

The basic example showcases its simplicity:

List<string> myList = new List<string>() {
  "Alpha",
  "Beta",
  "Gamma"
  };

foreach (string s in myList ) 
{
  Debug.Log($"Processing: {s}");
}

Conditional Logging

You can use a conditional statement in the body of your foreach loop. Use a conditional to to selectively log information based on specific criteria. In this example, items starting with ‘A’ are singled out for logging:

List<string> myList = new List<string>() {
  "Alpha",
  "Beta",
  "Gamma"
  };

foreach (string s in myList ) 
{
  if (s.StartsWith("""))
  {
    Debug.Log($"Found item starting with 'A': {s}");
  }
}

Logging Index and Value

The foreach loop does not include the index to the body. But, you may need the index to confirm that the code is executing properly. To access the index, you have two options:

  • Use a for loop.
  • Initialize an integer outside of the foreach scope, then increment it each iteration.

Here’s how to use a for loop:

List<string> myList = new List<string>() {
  "Alpha",
  "Beta",
  "Gamma"
  };

for (int i = 0; i < myList.Count(); i++)
{
  Debug.Log($"Index: {i}, Value: {myList[i]}");
}

📄 Resources:

Best Practices

Format Log Messages Appropriately

Format log messages using string interpolation or concatenation to ensure clarity and readability.

Avoid Excessive Logging

Exercise caution to maintain performance by avoiding excessive logging in production code.

Combine with Conditional Breakpoints

For interactive debugging, consider integrating Debug.Break() or conditional breakpoints to halt execution under specific conditions.

Conclusion

Using foreach loops with Unity’s Debug.Log method makes it easy for you to debug a list in Unity. The techniques that you have learned today will help you improve your game’s performance and reduce the prevalence of bugs in your project.

Free download: Indie Game Marketing Checklist

Download now

Category

programming

Don't forget to share this post!

Popular assets for Unity

See all assets ->
    Cutting-edge volumetric fog and volumetric lighting with support for transparent materials.
    Volumetric clouds, day night cycles, dynamic skies, global lighting, weather effects, and planets and moons.
    A lightweight procedural skybox ideal for semi-stylized projects.
    Image-based Outlines for 2D and 3D games with variable line weight, color, and displacement options.
    Drag-and-drop ready-to-use ambient, impact, and spell particle effects.
    Per-pixel gaussian blur on your entire screen, part of the UI, or in-scene objects.

Free Indie Game Marketing Checklist

Learn how to make your game successful with this handy checklist.

Download for free