Unity crashes after i run my save file script

1 day ago 1
ARTICLE AD BOX

Im trying to make a save file that save the position and speed of all objects in the scene, however when OnSave() is called, the unity editor completely freezes and doesnt seem to do anything

using System.Collections.Generic; using UnityEngine; using Newtonsoft.Json; using System.IO; using System; public class DataService : MonoBehaviour { string path = Directory.GetCurrentDirectory() + "/SaveFileGame.Json"; public SaveFile Saver; public List<GameObject> GB_list = new List<GameObject>(); public List<Vector3> VEL_list = new List<Vector3>(); public List<Vector3> TRANS_list = new List<Vector3>(); public void OnSave() { GB_list.Clear(); VEL_list.Clear(); TRANS_list.Clear(); var componentRB = GameObject.FindObjectsByType<Rigidbody>(FindObjectsSortMode.None);//Adds all rigidbody components for(int i = 0; i < componentRB.Length; i++){ if(! componentRB[i].gameObject.CompareTag("Player")){ //removes BIMOS Player objects VEL_list.Add(componentRB[i].linearVelocity);//Adds velocity to list GB_list.Add(componentRB[i].gameObject);//Adds GameObject to list TRANS_list.Add(componentRB[i].gameObject.GetComponent<Transform>().position);//Adds Transform To list } } Saver.SAVED_GB = GB_list; Saver.SAVED_VEL = VEL_list; Saver.SAVED_TRANS = TRANS_list; // applies all lists to SaveFile script to be easily re applied to Json if (File.Exists(path)){ File.Delete(path); string json = JsonConvert.SerializeObject(Saver, Formatting.Indented, new JsonSerializerSettings{ ReferenceLoopHandling = ReferenceLoopHandling.Ignore}); File.WriteAllText(path,json); //overwrites file if it already exists } else { File.Create(path); string json = JsonConvert.SerializeObject(Saver, Formatting.Indented, new JsonSerializerSettings{ ReferenceLoopHandling = ReferenceLoopHandling.Ignore}); //creates new file if it doesn't File.WriteAllText(path,json); } } }

I used the code below to prevent a self reference loop, is the loop just happening over and over again causing the crash, but being ignored to prevent the error?

JsonSerializerSettings{ ReferenceLoopHandling = ReferenceLoopHandling.Ignore});

edit: this is the save file script:

using UnityEngine; using BIMOS; using System.Collections.Generic; [System.Serializable] public class SaveFile { public List<GameObject> SAVED_GB = new List<GameObject>(); public List<Vector3> SAVED_VEL = new List<Vector3>(); public List<Vector3> SAVED_TRANS = new List<Vector3>(); }
Read Entire Article