Hello,
I made a unity project where I instantiate prefabs on click, and when I am done with the process I click on a save button and it should save the positions of all the prefabs that have been instantiated in a .txt file.
I have tried the following code that I got from another forum post, the code saves the position of whatever game object you attach it to. I have attached it to an empty game object that serves as the parent of all the clones being instantiated. So, the code only gives the position of the game object that it is attached to.
Here is the code I used:
public class savePosition : MonoBehaviour {
public Button SaveBtn;
string FILE_NAME = "positionFile.txt";
// Use this for initialization
void Start () {
SaveBtn.onClick.AddListener(() =>
{
float x = transform.position.x;
float y = transform.position.y;
float z = transform.position.z;
StreamWriter sw = File.AppendText(FILE_NAME);
sw.WriteLine("[ " + x + " , " + y + " , " + z +" ]");
sw.Close();
});
}
}
Here is the output:
[ -0.853 , 0.5 , 1 ]