- Home /
How to save/load the Y-Coordinates of Instantiated Objects
I have a grid of cubes that was too large for me to simply copy and paste. In order to make it, I needed to array a prefab using a for loop. These cubes are then translated up and down by the player using Raycasting. What I am trying to do is re-read the array, making a CSV of the Y-values with their changes. They don't move along any other axis, so I don't need to save them.
The intention is that later, the player can load their CSV and have the cubes return to the saved values.
This is the code I have to create the array, in case their is a better way to create it that would allow this functionality. As it stands, it saves the values, however this is only when everything loads.
using UnityEngine;
using System.Collections;
using System.IO;
public class FloorArray : MonoBehaviour{
public GameObject defObject;
GameObject defObject2;
public float xVal = -17.14359f;
public float yVal = -1.830479f;
public float zVal = -1.685268f;
//Variables used to save the coordinate values
public string[] arrVector1 = new string[150];
public string arrVector2;
public Vector3 Vector;
float fArr;
void Start () {
defObject = Instantiate(Resources.Load("Prefabs/FloorPin")) as GameObject;
for (float x = 0; x < 15; x += 0.1f)
{
for (float z = 0; z < 10; z += 0.1f)
{
Vector = new Vector3(xVal - x, yVal, zVal + z);
arrVector2 += yVal + ", ";
defObject2 = Instantiate(Resources.Load("Prefabs/FloorPin"), Vector, Quaternion.Euler(270, 0, 0)) as GameObject;
}
fArr = x * 10;
arrVector1[(int)fArr] = arrVector2;
}
//Saves the generated array into a string
string[,] strName = new string[15,10];
strName[0,0] = arrVector1.ToString();
}
void save()
{
System.IO.File.WriteAllLines(@"E:\Test\TestFile.csv", arrVector1);
}
}
I have attempted to use the method outlined on this website http://wiki.unity3d.com/index.php?title=Save_and_Load_from_XML, however System.IO.File.WriteAllLines(@"E:\Test\TestFile.csv", arrVector1);
doesn't accept it, giving me an error CS1502 (Best overloaded method match has some invalid arguments).
If anyone has any ideas it would be greatly appreciated.
Answer by zharik86 · May 12, 2014 at 07:49 AM
See this documentation from microsoft. In function WriteAllText is two argument of type string. In your case - second argument is array of string. This change to string. For example, see my code below(write on CSharp):
public void save() {
string tempStr = ""; //your string for file
for(int i = 0; i < arrVector1.Length; i++) {
if (tempStr!= "") { //add new line in our variable
tempStr = tempStr + "\n";
}
tempStr = tempStr + arrVector1[i];
}
System.IO.File.WriteAllLines(@"E:\Test\TestFile.csv", tempStr);
}
Thank you for your answer. I am still receiving the same "Error CS1502: The best overloaded method match for System.IO.File.WriteAllLines(string, string[])' has some invalid arguments". It says this is happening at line 114 (
System.IO.File.WriteAllLines(@"E:\Test\TestFile.csv", tempStr);` )
Is it beco$$anonymous$$g a 2D array when this:
arrVector2 += yVal + ", ";
gets put into this?
fArr = x * 150;
arrVector1[(int)fArr] = arrVector2;
@Adge Initially I was guided only by your error with saving. Having looked at the Start function more attentively, it is possible to tell that your array of arrVector2 is filled only partially. Whether you need to remember coordinates of all gameObject on a scene in the text document?
Thanks! I have it going through now and saving them. It goes through the array again and finds the changed Y coordinates, rather than writing the changes in real-time (to save CPU power and all that). So for some reason it is limiting the array length to 94 (if you happen to have an answer to that). Your answer definitely helped with getting the save function itself working properly, though! Thanks again!
using UnityEngine;
using System.Collections;
using System.IO;
public class FloorArray : $$anonymous$$onoBehaviour{
public GameObject defObject;
GameObject defObject2;
public GameObject frontDoorPin;
GameObject frontDoorPin2;
public float xVal = -17.14359f;
public float yVal = -1.830479f;
public float zVal = -1.685268f;
//Variables used to save the coordinate values
public string[] arrVector1 = new string[150];
public string arrVector2;
public Vector3 Vector;
float fArr;
GameObject[,] floorPinArray = new GameObject[200, 150];
// Use this for initialization
void Start () {
//Writes the floor pins as an array
//defObject = Instantiate(Resources.Load("Prefabs/FloorPin")) as GameObject;
for (float x = 0; x < 15; x += 0.1f)
{
for (float z = 0; z < 10; z += 0.1f)
{
Vector = new Vector3(xVal - x, yVal, zVal + z);
arrVector2 += yVal + ", ";
defObject2 = Instantiate(Resources.Load("Prefabs/FloorPin"), Vector, Quaternion.Euler(270, 0, 0)) as GameObject;
floorPinArray[(int)(x*10),(int)(z*10)] = defObject2;
}
//fArr = x * 10;
//arrVector1[(int)fArr] = arrVector2;
}
}
// Update is called once per frame
void Update () {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F3))
{
try
{
save();
print("YOUR COORDINATES HAVE BEEN SAVED!");
}
catch
{
Application.Quit();
print("Game died :(");
}
}
}
public void save() {
string[] tempStr = new string[200]; //your string for file
for(int i = 0; i < 94; i++)
{
tempStr[i] = " ";
for (int j = 0; j < 94; j++ )
{
tempStr[i] += floorPinArray[i, j].transform.position.y.ToString("G4") + ", ";
}
//tempStr[i] = tempStr[i] + arrVector1[i];
}
File.WriteAllLines("E:\\Test\\TestFile.csv", tempStr);
}
}