- Home /
Why wount my saving script save properly ?
Hi, im having a problem with my savig script.
the problem is that it will save the first two levels properly, but when i finish the third level it wount write to the file.
Why is it doing that ?
i also tried to add another level to my array of names but it looks like unity wount add it to the array.
ps. it saves the name of the level found in the array to a text file and then another script loads that level when a button is pressed.
The code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
public class saver : MonoBehaviour {
private string winDir;
//private List<string> read = new List<string>();
public string finishedLevel = "aa";
public string[] levels = {"level3", "2D Learning", "level2", "startMenu"};
private string[] entries;
//public GameObject loader;
//private levelLoader levelloader;
public int x = 0;
public bool nextLevel = false;
// Use this for initialization
void Start () {
winDir= Application.dataPath + "/save.txt";
//Read ();
//levelloader = loader.GetComponent<levelLoader> ();
//levels = levelloader.levels;
}
// Update is called once per frame
void Update () {
Read ();
Debug.Log ("update etter READ");
//Debug.Log ("winDir: " + winDir);
//Debug.Log ("FILE: " + read);
Debug.Log ("Entries[0aaa]: " + entries[0]);
//Debug.Log ("XAAA: " + x);
while(entries[0] != levels[x]) {
x += 1;
Debug.Log ("XaAA: " + x);
}
if (nextLevel == true) {
//x +=1;
//times +=1;
Write (levels[x +1]);
Read ();
nextLevel = false;
Debug.Log("Nextlevel");
//x = 0;
}
}
void Read(){
string line;
// Create a new StreamReader, tell it which file to read and what encoding the file
// was saved as
StreamReader theReader = new StreamReader(winDir, Encoding.Default);
// Immediately clean up the reader after this block of code is done.
// You generally use the "using" statement for potentially memory-intensive objects
// instead of relying on garbage collection.
// (Do not confuse this with the using directive for namespace at the
// beginning of a class!)
using (theReader)
{
// While there's lines left in the text file, do this:
do
{
line = theReader.ReadLine();
if (line != null)
{
// Do whatever you need to do with the text line, it's a string now
// In this example, I split it into arguments based on comma
// deliniators, then send that array to DoStuff()
entries = line.Split(',');
//if (entries.Length > 0)
// doneRead(entries);
}
}
while (line != null);
// Done reading, close the reader and return true to broadcast success
theReader.Close();
//return true;
}
}
//}
void Write(string levelDONE){
System.IO.File.WriteAllText(winDir, levelDONE);
}
/*
void doneRead(string[] entiress){
//Debug.Log (entiress[0]);
}
*/
}
Comment
Your answer
Follow this Question
Related Questions
Remembering the state of a scene 1 Answer
Load XML File ? 1 Answer
player prefs, problem but no problem??? 1 Answer
Perform action on save/load in editor 2 Answers
Save variable value. 1 Answer