- Home /
 
               Question by 
               nlgamers200 · Jan 09, 2021 at 02:26 PM · 
                filesaving  
              
 
              trying to store to json
 public void SaveToFile()
 {
     for (int i = 0; i < ListofObjects.transform.childCount;)
     {
         var pos = ListofObjects.transform.GetChild(i).transform.position;
         var rot = ListofObjects.transform.GetChild(i).transform.localEulerAngles;
         var scale = ListofObjects.transform.GetChild(i).transform.localScale;
         var name = ListofObjects.transform.GetChild(i).transform.name;
         i++;
     }
     SaveObject saveObject = new SaveObject
     {
         HowManyObjects = ListofObjects.transform.childCount,
     };
     string json = JsonUtility.ToJson(saveObject);
     Debug.Log(json);
 }
 private class SaveObject
 {
     public int HowManyObjects;
 }
Im trying to save the position, rotation, scale, and name of all children of ListofObjects to a son file. Could anyone help me out?
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by Hellium · Jan 09, 2021 at 02:41 PM
 public void SaveToFile()
 {
     GameObjectDataCollection gameObjectDataCollection = new GameObjectDataCollection();
     for (int i = 0; i < ListofObjects.transform.childCount; ++i)
     {
         Transform child = ListofObjects.transform.GetChild(i);
         gameObjectDataCollection.Add(new GameObjectData(child.gameObject));
     }
     string json = JsonUtility.ToJson(gameObjectDataCollection);
     Debug.Log(json);
     System.IO.File.WriteAllText(Application.persistentDataPath + "/file.json", json);
 }
 
 [System.Serializable]
 public class GameObjectData
 {
     public string Name;
     public Vector3 Position;
     public Vector3 LocalEulerAngles;
     public Vector3 LocalScale;
     public GameObjectData(GameObject gameObject)
     {
         Name = gameObject.name;
         Position = gameObject.transform.position;
         LocalEulerAngles = gameObject.transform.localEulerAngles;
         LocalScale = gameObject.transform.localScale;
     }
 }
 
 [System.Serializable]
 public class GameObjectDataCollection
 {
     public List<GameObjectData> GameObjectData = new List<GameObjectData>();
     
     public void Add(GameObjectData gameObjectData)
     {
         GameObjectData.Add(gameObjectData);
     }
 }
Your answer
 
 
             Follow this Question
Related Questions
Loading a saved Game 1 Answer
Windows 8 - Windows.Storage WriteTextAsync exception 0 Answers
How to add existing xml file to project build? 1 Answer
When writing a txt file, it says sharing violation 3 Answers
Sharing Violation On Path 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                