- Home /
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>();
}
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.
@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
Follow this Question
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