Saving the names of objects in a List to an external file

Hello!
I am building an app that lets a user dynamically add and remove gameobjects from a scene. The gameobjects are currently being saved to a public List named furniture.
I am wanting to allow users to save the names of the gameobjects within furniture to an external file, such as XML, CSV or JSON. I have been experimenting with JsonUtility.ToJson(this) and Sinbad.CsvUtil.SaveObjects but I’m struggling to get this to work.

Here is my current code:

void pickingListTaskOnClick ()
    {
        foreach (GameObject go in furniture)
        {
            furnitureNames.Add (go.name);
            Debug.Log(go.name); 
        }

        SaveToString ();

        //Sinbad.CsvUtil.SaveObjects (furnitureNames, "pickingList.csv");
    }

    public string SaveToString ()
    {
        return JsonUtility.ToJson (furnitureNames);
    }

To potentially add complications, the user will be running IOS or Android as this is a mobile app.

Any help would be appreciated, thanks!

Hi @danbrownemotivevm ,
First of all you need a class in which your data should be store and then pass this class object to JsonUtility.ToJson method so after that you can save your data into json file.

Hi @danbrownemotivevm

As @ajaykewat said, you need a class in which your data will be stored.

Create a new C# script. Let’s say DataManager.cs

And inside this script create another class that will serializae your data

Here is an example (not tested):

public class DataManager : MonoBehaviour
{
    public static DataManager i;

    private void Awake()
    {
        if (i != null)
        {
            Destroy(gameObject);
        }
        else
        {
            i = this;
            DontDestroyOnLoad(gameObject);
        }
    Load();
    }

    public void Load()
    {
        DataModel.Load();
    }
    void OnApplicationPause(bool pauseStatus) 
    {
        if(pauseStatus) {
            DataModel.Save();
        }
    }

    public void OnApplicationQuit() 
    {
        DataModel.Save();
    }

    public void Trigger_ButtonSave() 
    {
        DataModel.Save();
    }

    public DataModel DataModel = new DataModel("save");

}

[System.Serializable]
public class DataModel
{
        public List<string> furnitureNames;
    
        private string name;
    
        public DataModel(string _name)
        {
            this.name = _name;
        }
    
        public void Save()
        {
           
            string json = JsonUtility.ToJson(this);
            
            System.IO.File.WriteAllText(System.IO.Path.Combine(Application.persistentDataPath, "save.json"), json);
        }
    
        public void Load()
        {
            string jsonToFile = System.IO.File.ReadAllText(System.IO.Path.Combine(path, "save.json"));
    
            DataModel packedData = JsonUtility.FromJson<DataModel>(jsonToFile);
    
            this.furnitureNames= packedData.furnitureNames;
    
        }
    
        public void SetFurniture(string newFurniture)
        {
             furnitureNames.Add(newFurniture);
        }
        public void GetFurniture(int i)
        {
            return furnitureNames*;*

}
}