Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ItsmeKerboy · Oct 05, 2018 at 05:35 AM · unity 5serializationserializeserializable

Save and Load all spawned objects in their position using array

Hi I am having a problem, all I want to do is save all spawned object and their position and then load it. I used array and for loops but still not working. All I can do is save and load a single objects position.

This is my code.

Respawn tag is the spawned objects.

 using System;
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class SaveTable : MonoBehaviour {
     float x;
     float y;
     float z;
     Vector3 TablePosition;
     GameObject TableObject;
     int TableNum;
     Vector3 TablesInWholeArea;
     List<float> xTables = new List<float>();
     List<float> yTables = new List<float>();
     List<float> zTables = new List<float>();
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 
 
     public void TableSave()
     {        
      TableObject = GameObject.FindWithTag("Respawn");
      BinaryFormatter bf = new BinaryFormatter();
      FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
 
      GameSave Save = new GameSave();
      TableNum = GameObject.FindGameObjectsWithTag("Respawn").Length;
      for(int i = 0; i < TableNum; i++)
      {    
      TablePosition = GameObject.FindWithTag("Respawn").transform.position;
      x = TablePosition.x;
      y = TablePosition.y;
      z = TablePosition.z;
      xTables.Add(x);
      yTables.Add(y);
      zTables.Add(z);
      
      
      }
 
     foreach(float str in xTables)
      {
        Debug.Log(str);
      }
      
      Save.x = x;
      Save.y = y;
      Save.z = z;
      Save.TableNum = TableNum;
      Save.xTables = xTables;
      Save.yTables = yTables;
      Save.zTables = zTables;
      bf.Serialize(file, Save);
      file.Close();
 
      }
 
     public void TableLoad()
     {
 
         if(File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
         {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
             GameSave Save = (GameSave)bf.Deserialize(file);
             file.Close();
 
             for(int i = 0; i < Save.TableNum; i++)
             {
                 Instantiate(TableObject, new Vector3(Save.xTables[i],Save.yTables[i],Save.zTables[i]), Quaternion.identity);
             }
             
         }
 
     }    
 
 }
 
 
 
 
 
 
 [System.Serializable]
 public class GameSave {
 public float x;
 public float y;
 public float z;
 public int TableNum;
 public List<float> xTables = new List<float>();
 public List<float> yTables = new List<float>();
 public List<float> zTables = new List<float>();
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by LCStark · Oct 05, 2018 at 07:02 AM

TableNum = GameObject.FindGameObjectsWithTag("Respawn").Length;
TablePosition = GameObject.FindWithTag("Respawn").transform.position;

You are getting all of your Respawn tagged objects, but you only use that array to get its Length. Then you find the first Respawn object and you save its position to your file, along with the array length.

Check this answer to find out how to loop through all of your objects: https://answers.unity.com/questions/50208/loop-through-all-objects-with-same-tag.html

You'll need to create a GameSave for all of your objects and save them all to your file.
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image UnityCoach · Oct 05, 2018 at 07:29 AM 0
Share

@LCStark is right. I would add that I would avoid using FindObject methods in Update(), these have a bit of a cost. Try keeping everything in a static List, to which you can easily add when you spawn new objects.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

186 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Saving Non-Prefab Data During Editor Play 0 Answers

Private Variables Serialised By Default 2 Answers

Serialized data show information after load 0 Answers

Can a GameObject be serialized? 1 Answer

Saving Game Confussion 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges