The question is answered, right answer was accepted
write to new line?
Sounds kinda simple but can't seem to get it to work.
Currently my game is asking for a name, upon a button click, this name and their score are stored in an array. I want this array to be saved to a text file, which it is, but it only stores 1 game save if you can understand that.
So the original text is getting written over each time the score and name are saved.
Here is what I'm trying:
using UnityEngine; using System; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine.UI; using UnityEngine.EventSystems;
public class GUI_Button : MonoBehaviour {
public PlayerMovement GUIScript;
public RandomSpawn GUIScript1;
private int count = RandomSpawn.count;
public string PlayerName = "Enter name";
public string fileName = "leaderboard.txt";
public string FinalName = "";
public string count1 = "";
public string newline = "\n";
void OnGUI ()
{
if (PlayerMovement.livess <= 0)
{
GUI.Button (new Rect (400, 200, 300, 200), "Score:" + (count+10));
PlayerName = GUI.TextField (new Rect (400, 400, 300, 100), PlayerName);
CountToString ();
if (GUI.Button (new Rect (400, 500, 200, 100), "Save Score"))
FinalName = PlayerName;
StoreInArray ();
}
}
void CountToString ()
{
count1 = count.ToString();
}
void StoreInArray ()
{
string[] PlayerAndScore = {FinalName, count1};
System.IO.File.WriteAllLines(@"C:\Users\Plum\Desktop\Pixel Ninjav3.3\Assets\leaderboard.txt", PlayerAndScore);
System.IO.File.AppendAllText(@"C:\Users\Plum\Desktop\Pixel Ninjav3.3\Assets\leaderboard.txt", newline);
}
}
Answer by Roddeck · Mar 19, 2016 at 03:33 PM
void StoreInArray(){
string[] PlayerAndScore = {FinalName, count1};
StreamWriter w = File.AppendText( /path to file/);
w.Write(PlayerAndScore);
w.Close();
}
Hope this helps
This comes back with "System.String []" when written to the file
You need to write to file per line in a loop so something like:
foreach (string line in PlayerAndScore){
w.Write(line);
}
Or if this writes all in one line do:
foreach (string line in PlayerAndScore){ w.Write(line); w.Write("\n"); }
Let me know if that works ;)
Follow this Question
Related Questions
save one big json string vs multi seperate json strings in playerprefs 0 Answers
How to convert the dynamic list into a array and save it to next scene. 0 Answers
How do I read and write data? 1 Answer
Cannot Download files to Android device (v5.3.5f1) 1 Answer
Cannot Save due to file access denied WINDOWS 8 COMPUTER 0 Answers