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:
{
"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
ToStringfor yourExperimentclass 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!");