- Home /
How do you save the position of clone prefabs in a text file?
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 ]
Do you want to store the position of each clone in the same file? Then you should either store all the instantiated game objects in the script that instantiates them, and when the button is pressed loop through the existing objects (!= null), or when pressing the save button find all the saveables (those that have savePosition script on them) by calling savePosition[] saveables = FindObjectsOfType<savePosition>();
, and save those to a file (NOTE: FindObjectsOfType() is expensive!).
Yeah i want to store them in the same file. the problem with the instantiated objects is that they dont have the savePosition script on them unlike the parent which has it before i ran the project. is there a way to have the script attached to the prefabs beforehand?
Sorry i am still a beginner.
Answer by Clavus · Feb 13, 2018 at 03:32 PM
So you want to store the positions of just the children of the empty game object with the script correct? Something like this should work:
public class savePositionOfChildren : MonoBehaviour {
public Button SaveBtn;
string FILE_NAME = "positionFile.txt";
// Use this for initialization
void Start () {
SaveBtn.onClick.AddListener(() =>
{
StreamWriter sw = File.AppendText(FILE_NAME);
foreach(Transform child in transform) {
float x = child.position.x;
float y = child.position.y;
float z = child.position.z;
sw.WriteLine("[ " + x + " , " + y + " , " + z +" ]");
}
sw.Close();
});
}
}
Hello, Thanks for the quick reply. I have tried the modification that you made, it still saves the position of the empty game object as many times as there are clones in the scene.
Are you sure the prefabs are not positioned at 0,0,0 relative to their parent?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Saving Player Data 3 Answers
How would I make a ListChangedEvent? 1 Answer
How do I make a 3d person camera that changes with keyboard based input 1 Answer