- Home /
The question is answered, right answer was accepted
Deleting a file inside a BeginArea and BeginHorizontal
Right now I'm trying to delete a save file.
My save files are loaded within a foreach loop and hooked to buttons. Tapping the button loads the corresponding level.
I realized I had no way to delete these levels, and so I tried to implement what I thought would be pretty easy.
void OnGUI()
{
GUILayout.BeginArea(new Rect(Screen.width*.545f,(Screen.height*.25f+scrollPosition.y*-1),Screen.width*.45f,Screen.height*.7f));
GUILayout.Label("Saved maps");
foreach (var f in dir.GetFiles("*.dm"))
{
string del = ",";
string text = System.IO.File.ReadAllText(f.FullName);
string[] lines = Regex.Split(text, "\n");
string[] cells = Regex.Split(lines[lines.Length-2], del);
GUILayout.BeginHorizontal();
if(GUILayout.Button (cells[1]+":"+cells[0]+" "+cells[4], GUILayout.Width(Screen.width*.349f), GUILayout.Height(Screen.height*.06f)))
{
StartCoroutine("LoadOld");
//Application.LoadLevel("Table");
}
if(GUILayout.Button ("Del", GUILayout.Width (Screen.width*.349f), GUILayout.Height(Screen.height*.06f)));
{
File.Delete(f.FullName);
}
GUILayout.EndHorizontal();
}
GUILayout.EndArea();
}
The problem occurs when I add the second button (including both buttons in a Horizontal layout) and suddenly when ever the scene loads it deletes all the files without any user input. What's going wrong here?
Answer by RudyTheDev · Feb 10, 2014 at 09:54 PM
You have a "`;`" after your if(GUILayout.Button ("Del", GUILayout.Width (Screen.width*.349f), GUILayout.Height(Screen.height*.06f)));
, which means { File.Delete(f.FullName); }
always executes.
Unbelievable. I just...I'm walking away from program$$anonymous$$g the rest of the day. Thanks, I probably never would have caught that.