How to log an array?

New to Unity, not new to software engineering. I’m looking to log an array (without bloating the code), and this is turning out to be a challenging and complicated task.

In other languages, it would be trivially easy. For example, in js you’d simply convert the array into a JSON.

I see there are C# packages that let you do similarly, but for all I know there might be a simple and elegant solution that I do not know about.

Since you have already necroed this thread , have you actually read my post which was the last one? Of course JSON (javascript object notation) is available in javascript. For most other languages there usually are packages or built-in json frameworks. However unlike javascript most languages which are more sophisticated and closer to the hardware have much more datatypes than javascript. So usually only a subset can be serialized. So just logging an “array” doesn’t mean much as not every possible type can be serialized.

In C# every type has a ToString method. However most complex types (classes or structs) have to explicitly implement / override this method or it will just return the type name. In my examples I specifically used ToString on the array members.

It sounds like perhaps you would be happier if you went and coded in those languages.

Are you struggling to convert data? Is that what’s happening here?? Because that’s actually what it looks like.

Rather than colorizing everything with “it would be trivial” and “without bloating the code” and other meaningless observations and conditions, try this structure to report problems to complete strangers in an international engineering forum:

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

  • Do not TALK about code without posting it.
  • Do NOT post unformatted code.
  • Do NOT retype code. Use copy/paste properly using code tags.
  • Do NOT post screenshots of code.
  • Do NOT post photographs of code.
  • ONLY post the relevant code, and then refer to it in your discussion.

The above speaks volumes of where you are on your journey. I hope you move forward out of this difficult area quickly.

As Mike Acton observed:

“The purpose of all programs, and all parts of those programs, is to transform data from one form to another.”

My apologies for not providing code snippets and clarifying what I meant by “bloated”. By bloated, I simply mean writing loops and logging in each iteration.

Also, not sure how to interpret all of this other than that its clear that you two are angry at me for saying negative things about C# and Unity both here and in the other thread we were arguing on.

Code snippet:

Javascript, if logging to a backend or IDE console: console.log(JSON.stringify(myArray))
Or, if logging to the Chrome console: console.log(myArray)

C#: Debug.Log(???); // something approximately as simple and fast to type

myArray.Foreach(x => Debug.Log(x));

Code snippet

It seems that the .ForEach method exists on lists but not arrays, but that works perfectly for lists

Usage

Debug.Log(myCollection.StringJoin()); // "item1, item2, item3"
Debug.Log(myCollection.StringJoin("\n")); // "item1\nitem2\nitem3"
Debug.Log(myCollection.StringJoin(obj => obj.someInteger.ToString()));// "23, 7, 47"

Source

using System.Linq;
using System.Collections.Generic;

public static class StringUtil
{
  public static string StringJoin<T>(this IEnumerable<T> collection, string separator = ", ")
  {
      return collection != null ? string.Join(separator, collection) : "NULL COLLECTION";
  }

  public static string StringJoin<T>(this IEnumerable<T> collection, System.Func<T, string> selector,
      string separator = ", ")
  {
      return collection != null ? string.Join(separator, collection.Select(selector)) : "NULL COLLECTION";
  }
}

Class Array contains ForEach aswell

Array.ForEach(array, i => Debug.Log(i));

For anyone who wants to just enter one line of code:

Debug.Log(string.Join(", ", yourArray);

Lo-renzo’s answer adds validation and versatility.

To operate on an array you would need to

Array.ForEach(myArray, x => Debug.Log(x));

List<T>.ForEach isn’t from the LINQ extenions: List<T>.ForEach(Action<T>) Method (System.Collections.Generic) | Microsoft Learn