Save whats in GUI.Text field to variable
Hi, I need to save what the player enters to the text field, and then write that to a file so I'm able to store the playername and score to the file and then sort them, so I'm able to read from the file and display the name with the score on a different scene.
Ideally the player would press a button "Save score" and then the playername would be written to the variable.
I'm trying to make a leaderboard but I'm finding it quite difficult right now.
Pretty sure it's the new UI.
This is what I've done, It's messy.
public Player$$anonymous$$ovement GUIScript;
public RandomSpawn GUIScript1;
private int count = RandomSpawn.count;
public string PlayerName = "Enter name";
5. public string fileName = "leaderboard.txt";
string[] PlayerAndScore = new string [1];
public string PlayerNameFinal;
public string count1 = "";
private bool SaveScoreButton = false;
10.
void OnGUI ()
{
if (Player$$anonymous$$ovement.livess <= 0)
{
15. GUI.Button (new Rect (400, 200, 300, 200), "Score:" + (count+10));
PlayerName = GUI.TextField (new Rect (400, 400, 300, 100), PlayerName);
SaveScoreButton = GUI.Button (new Rect (400, 500, 200, 100), "Save Score");
CountToString ();
}
20. }
void OnClick()
{
PlayerNameFinal = PlayerName;
}
25. void Start()
{
PlayerAndScore [0] = PlayerNameFinal;
PlayerAndScore [1] = count1;
System.IO.File.WriteAllLines(@"Desktop\Pixel Ninjav3.3\Assets\leaderboard.txt", PlayerAndScore);
30. }
void CountToString ()
{
count1 = count.ToString();
}
$$anonymous$$ake any sense to you @hexagonius ?
$$anonymous$$y original thoughts on this included me making a GUI text field which the player will be able to write to, this will then be saved to a variable. The player will press ‘Save game’ and the variable stored in the text field will be moved to a permanent variable. From here, I will put the name variable and score into an array, which will be stored in an array in text file. I will then make a script to sort the array in the text file and output the {0} and {1} values for the highest score, {2} and {3} for 2nd highest score etc.
Using "void OnGUI()" is the old UI. The new UI uses real screen elements. SaveScoreButton is the status of the button when clicked. Since OnGUI is called multiple times per frame, you should execute what you want to do on button click right there ins$$anonymous$$d of saving the bool.
Answer by plumel · Mar 17, 2016 at 06:13 PM
@hexagonius I think this might be it? But im getting an error (in orange) "Possible mistaken empty statement on line "if (GUI.Button (..." aka savescore button
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"));
{
PlayerNameFinal = PlayerName;
PlayerAndScore [0] = PlayerNameFinal;
PlayerAndScore [1] = count1;
Debug.LogError (PlayerAndScore);
System.IO.File.WriteAllLines(@"leaderboard.txt", PlayerAndScore);