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<em>,Save.yTables_,Save.zTables*), Quaternion.identity);*_</em>
* }*
* }*
* } *
}
[System.Serializable]
public class GameSave {
public float x;
public float y;
public float z;
public int TableNum;
public List xTables = new List();
public List yTables = new List();
public List zTables = new List();
}