I’m trying to code a saving system where it takes a variable from each item that contains the tag “planet2” and save it to a text file. I have a good ammount coded, but the problem is that when I run it, and it saves. I open the text file only to find that it is empty.
var pla : GameObject[];
var directory = "Saves";
var save1 = "Saves/planets.txt";
function Start () {
if (!System.IO.Directory.Exists(save1) ){
System.IO.Directory.CreateDirectory(directory);
save();
}
}
function Update () {
savingtimer ++;
if(savingtimer >= 5000){
save();
savingtimer = 0;
}
}
function save(){
if(pla == null){
pla = GameObject.FindGameObjectsWithTag("planet2");
}
for(var planet2 in pla){
planets.Add(planet2);
}
planets.Add (GameObject.FindGameObjectsWithTag("planet2"));
for(var pln :GameObject in pla){
var sw = new StreamWriter(save1);
var pls : planetbehave = pln.GetComponent(planetbehave);
sw.WriteLine("" + pls.pname + "
");
sw.Flush();
sw.Close();
}
}