Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot convert from 'System.Collections.Generic.List<Object>' to 'UnityEngine.Object'

I am quite new to Unity and C#, and I am trying to parse a json file to an Experiment object in Unity, but I keep getting this error message from the Debug.Log line saying that;

Argument 2: cannot convert from 'System.Collections.Generic.List<Experiment>' to 'UnityEngine.Object'

This is my load json code, using the JSON .NET for Unity asset. My json file is structured as an array of objects.

public void LoadJson()
  {
    using (StreamReader r = new StreamReader("exMercury.json"))
    {
      string json = r.ReadToEnd();
      List<Experiment> items = JsonConvert.DeserializeObject<List<Experiment>>(json);
      Debug.Log("Experimenter ", items);
    }
  }

The json file:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
  "experiments": [
    {
      "Title": "A title",
      "Ingredients": "list of ingredients",
      "Description": "description",
      "Points": 100,
      "Completed": false,
      "Steps": [
        "step 1",
        "step 2",
        "step 3",
      ],
      "Hypothesis": "",
      "Explanation": "",
      "Progress": 0.0
    },
]
}

The struct for experiments;

using System.Collections;
using System.Collections.Generic;
using Firebase.Firestore;

[FirestoreData]
public struct Experiment
{
  [FirestoreProperty]
  public string Title { get; set; }

  [FirestoreProperty]
  public string Description { get; set; }

  [FirestoreProperty]
  public List<string> Ingredients { get; set; }

  [FirestoreProperty]
  public List<string> Steps { get; set; }

  [FirestoreProperty]
  public int Points { get; set; }

  [FirestoreProperty]
  public bool Completed { get; set; }

  [FirestoreProperty]
  public string Hypothesis { get; set; }

  [FirestoreProperty]
  public string Explanation { get; set; }

  [FirestoreProperty]
  public float Progress { get; set; }
}

Any help would be appreciated 🙂

>Solution :

Debug.Log takes as parameters

public static void Log(object message, Object context);

where Object refers to UnityEngnie.Object and well as the error tells you a List<Experiment> is not a UnityEngine.Object.

The actual purpose of mentioned UnityEngine.Object context parameter is the following:

If this is e.g. called in a MonoBehaviour you can add

Debug.Log(someMessage, this);

using the component itself as context meaning when you click on the logged message once in Unity the according object will get highlighted in the Hierarchy or assets (for ScriptableObjects etc). This way you can pass in anything inheriting from UnityEngine.Object (GameObject, Component, ScriptableObject, Texture2D, Material, ….) and make it pingable.


Now I can of course only guess but I think what you actually want to achieve would be

  • Implement ToString for your Experiment class so it prints out the desired information

  • And then use e.g.

    // convert all items into their according string representation
    // see https://docs.microsoft.com/dotnet/api/system.linq.enumerable.select
    var strings = items.Select(i => i.ToString()).ToArray();
    // combine all individual string into a single one using whatever separator character you want
    // see https://docs.microsoft.com/dotnet/api/system.string.join
    var debugString = string.Join("\n\n", strings);
    // finally log this
    Debug.Log("Experiments: \n\n" + debugString);
    

or if you just want to check if it worked at all you could probably simply do

Debug.Log($"Successfully loaded {items.Count} Experiments!");
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading