- Home /
 
               Question by 
               Goubermouche · Aug 29, 2020 at 03:21 PM · 
                prefabssavingdata storagefoldersondestroy  
              
 
              saving prefabs in the onDestroy method
hello! I'm currently working on a prefab saving system (i want to save newly instantiated prefabs) my problem - I'm storing the prefabs in a list and then trying to save the list in the onDestroy method (which from my understanding activates when the scenes switch/the game shuts down) however it only saves the object with the highest index in the list.
this is my first time working on a saving system (apart from a very basic playerprefs-based system) and any advice would be highly appreciated!
 public GameObject obj;
 public List<GameObject> Obj;
 public int index;
 public string localPath;
 // Start is called before the first frame update
 void Start()
 {
 }
 // Update is called once per frame
 void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         Vector3 mousePos = Input.mousePosition;
         mousePos.z = 2.0f;       
         Vector3 objectPos = Camera.main.ScreenToWorldPoint(mousePos);
         var newObject = Instantiate(obj, objectPos, Quaternion.identity);
         newObject.name = index.ToString();
         index++;
         Obj.Add(newObject);
         localPath = "Assets/SaveData/" + newObject.name + ".prefab";
     }
 }
 private void OnDestroy()
 {
     for (int i = 0; i < Obj.Count; i++)
     {
         Debug.Log(Obj[i]);
         PrefabUtility.SaveAsPrefabAsset(Obj[i], localPath);
     }
 }
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                