The question is answered, right answer was accepted
Not writing to file correctly
Hi, I want the code to write PlayerNameFinal and count to an array in leaderboard.txt when SaveScoreButton is pressed.
This is currently what is being written to the file: System.Collections.Generic.Dictionary`2[System.String,System.Int32]
Code:
using UnityEngine; using System.Collections; using System.Collections.Generic; using System.IO; using Vexe.FastSave; using Vexe.Runtime.Types;
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";
string[] PlayerAndScore = new string [1];
public string PlayerNameFinal;
public string count1 = "";
private bool SaveScoreButton = false;
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);
SaveScoreButton = GUI.Button (new Rect (400, 500, 200, 100), "Save Score");
CountToString ();
}
}
void OnClick()
{
PlayerNameFinal = PlayerName;
}
void Start()
{
PlayerAndScore [0] = PlayerNameFinal;
PlayerAndScore [1] = count1;
System.IO.File.WriteAllLines(@"Desktop\Pixel Ninjav3.3\Assets\leaderboard.txt", PlayerAndScore);
}
void CountToString ()
{
count1 = count.ToString();
}
}
Answer by Brocccoli · Mar 16, 2016 at 06:55 PM
This code is strange.
For one, you declare a string[] of length 1 at the top, and then you add 2 strings to the array. This code should throw an IndexOutOfBoundsException before it even gets to the IO call.
Perhaps try fixing the size of your PlayerAndScore array to:
string[] PlayerAndScore = new string[2];
Although I'm not sure how much that will help you, as I'm totally boggled as to how you get an output in the first place.
It turns out I was looking at the wrong thing and nothing was output. I changed the size of the array, nothing else has happened ;_;
So you changed the size of the array, and now you get no output?
Can you set a breakpoint? Do you ever get to the IO call?
Where would you suggest putting the breakpoint? And IO call? Sorry but I really could do with some guidance on what to do.
Looking at it now, the breakpoint should definitely get hit as its in the Start() method (doh). Is the script attached to a GameObject? Either way, set a breakpoint on the System.IO.File.WriteAllLines(...) line and check the values of the string array when it gets hit. $$anonymous$$aybe the string array is filled with empty strings.
When I say IO call, I just meant the line you were using to write to the file.
Well... the break point.. This is what comes up
Also, this freezes unity.
I tried putting in a Debug.LogError to the PlayerAndScore variable.. This is what it says before the GUI buttons come up and after typing something in
System.String[] UnityEngine.Debug:LogError(Object) GUI_Button:Start() (at Assets/Scripts/GUI_Button.cs:38)
You can't just print out "PlayerAndScore" that's just going to print out "Object" as you see.
When you hit the breakpoint, you should be able to mouse over PlayerAndScore and dig into the array to see the values that are stored.
So I've made the break point
And I've run the game. Nothing is happening in monodevelop or the game
Also, was more or less coding this in a blind rage. This is for my computer science coursework and I haven't the foggiest how im gonna get it done in time
@Brocccoli thank you for all your help. really.
I have the command set on the save game button. upon it being pressed, the array is written to the file.
THAN$$anonymous$$ YOU SO $$anonymous$$UCH
I'm very happy to hear you got it working.
Good luck in the future!