hi Guys, i have a problem and i don’t know where it is ,can you help me ? so i have wrote this script for save all gameObject in scene in file json the problem is that the list is null why ?
this is the script :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class JsonManager : MonoBehaviour
{
string json;
GameObjectInScene gameObjectInScene;
public List gameObjectList;
[Header(“Check this for save level”)]
public bool save = false;
void Awake()
{
gameObjectList = new List();
}
void Start()
{
if (save)
{
SaveJson();
}
}
void SaveJson()
{
foreach (GameObject gObject in GameObject.FindObjectsOfType())
{
gameObjectInScene = new GameObjectInScene(gObject.name, gObject.transform.localScale, gObject.transform.position, gObject.transform.rotation);
gameObjectList.Add(gameObjectInScene);
}
json = JsonUtility.ToJson(gameObjectList);
}
}
public class GameObjectInScene : MonoBehaviour
{
public string name;
public Vector3 scale;
public Vector3 position;
public Quaternion rotation;
Wrap the list in a class. From the JsonUtility docs:
…passing an array to this method will not produce a JSON array containing each element, but an object containing the public fields of the array object itself (of which there are none) [i.e., null]. To serialize the actual content of an array or primitive type, it is necessary to wrap it in a class or struct.
(p.s. - In future posts, please use code tags. They make it easier for others to read your code.)
Thank you man i have done in this way :
[Serializable]
public class GameObjectInScene
{
public string name;
public Vector3 scale;
public Vector3 position;
public Quaternion rotation;
I wanted to follow up on this. I’m trying to do something similar. Working on a more basic project to build the more complex one.
I have the following
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class JSONRecorder : MonoBehaviour {
private string path;
private string filename = "MyList.JSON";
public int id;
public string nameString;
public double doubleNum;
public MyClass myClass;
public List<MyClass> myClassList;
void Start () {
myClassList = new List<MyClass>();
path = Application.persistentDataPath + "/" +filename;
}
void RecordIdName(int i, string s) {
myClass = new MyClass(i, s);
myClassList.Add(myClass);
string contents = JsonUtility.ToJson(myClassList);
File.WriteAllText (path, contents);
}
}
I have MyClass scripted out, too.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class MyClass{
public int identity;
public string theName;
public double doubleNumber;
public MyClass(int id, string name){
identity = id;
theName = name;
}
public MyClass(int id, string name, double num){
identity = id;
theName = name;
doubleNumber = num;
}
}
The JSON file is just blank brackets { }
When I debug myClass it shows “identity”:23,“theName”:BubbaGump,“doubleNumber”:0
Just as it should.
when I debug the myClassList it shows: System.Collections.Generic.List`1[MyClass]
Clearly something is going wrong in my list, but can’t figure it out, especially where it looks like the code is the same.