Reading from file for leaderboard
Hi
So far, I've got a script that allows a user to enter their name, its then stored with their score in an array, then this is stored in a text file.
So far, the text file has the layout: PlayerName Score
I was wondering how I would go about reading from the file to make a leaderboard from it, and also how to make it so that I can save more than 1 score in the file?
Many thanks, Plumel
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 = "";
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);
}
}
I was thinking after the i could write System.IO.File.AppendAllText(@"C:\Users\Plum\Desktop\Pixel Ninjav3.3\Assets\leaderboard.txt", newline);
and the string for new line is: public string newline = "\n";
Your answer
Follow this Question
Related Questions
Reading/writing changing integer variable. (JS) 0 Answers
Can AssetDatabase be used to load and store assets from a standalone game? 0 Answers
Adding to a text file. It works but I get this message afterwoods 1 Answer
The name 'HighScoreList' does not exist in the current context 1 Answer
How do I read and write data? 1 Answer