Deleting a saved Profile

I am making a Save feature for my game. This far making a profile and loading a profile is going well. Deleting a profile is proving more annoying though.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public static class SaveLoad
{
    public static List<Game> savedGames = new List<Game> ();

    public static void Save()
    {
        savedGames.Add (Game.current);
        BinaryFormatter bf = new BinaryFormatter ();
        FileStream file = File.Create(Application.persistentDataPath + "/SavedGames.gd");
        bf.Serialize (file, SaveLoad.savedGames);
        file.Close ();
    }

    public static void Load()
    {
        if (File.Exists (Application.persistentDataPath + "/savedGames.gd"))
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + "/savedGames.gd", FileMode.Open);
            SaveLoad.savedGames = (List<Game>)bf.Deserialize(file);
            file.Close();
        }
    }

    public static void Delete()
    {
        if (File.Exists (Application.persistentDataPath + "/savedGames.gd"))
        {
            savedGames.Remove (Game.current);
        }
    }
}

This is my code for making a new profile, loading one, and deleting one. All work fine other than the delete. I have tried this as well:

            savedGames.Remove (Game.current);
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + "/savedGames.gd", FileMode.Open);
            SaveLoad.savedGames = (List<Game>)bf.Deserialize(file);
            file.Close();

Just can’t seem to figure it out. Any and all help would be great! And thank you for reading.

Have you tried to call the Delete then the Save then Load?

No, I will give it a go!

Try that and let me know if it works or doesn’t work so we can figure it out!

Nope, didn’t like that:

Script Used:

public static void Delete()
    {
        if (File.Exists (Application.persistentDataPath + "/savedGames.gd"))
        {
            savedGames.Remove (Game.current);
            Save ();
            Load ();
        }
    }

I just noticed, that the file name is different you have an upper case in one and a lower case in an other.

Where am I doing that? Sorry, just not seeing it.

Lines 15, 22, 33

Oh my god I am a moron XD Cheers for finding that. It has fixed a few of my other issues. But when deleting it still gives me an error:

It does delete the profile, but yells at me.

Ok I have everything work for the most part. I do still have some problems.
Problem 1: I can’t see the profiles in the delete menu unless I go to load first.
Problem 2: For some reason it seems that saving a profiles progress sometimes erases the other profiles and some times it doesn’t.

Just wondering if anyone can understand why. Here are the scripts I am using:

Script for Saving, Loading, and Deleting:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public static class SaveLoad
{
    public static List<Game> savedGames = new List<Game> ();

    public static void Create()
    {
        savedGames.Add (Game.current);
        BinaryFormatter bf = new BinaryFormatter ();
        FileStream file = File.Create (Application.persistentDataPath + "/SavedGames.gd");
        bf.Serialize (file, SaveLoad.savedGames);
        file.Close ();
    }

    public static void Save()
    {
        BinaryFormatter bf = new BinaryFormatter ();
        FileStream file = File.Create (Application.persistentDataPath + "/SavedGames.gd");
        bf.Serialize (file, SaveLoad.savedGames);
        file.Close ();
    }

    public static void Load()
    {
        if (File.Exists (Application.persistentDataPath + "/SavedGames.gd"))
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + "/SavedGames.gd", FileMode.Open);
            SaveLoad.savedGames = (List<Game>)bf.Deserialize(file);
            file.Close();
        }
    }

    public static void Delete()
    {
        if (File.Exists (Application.persistentDataPath + "/SavedGames.gd"))
        {
            savedGames.Remove (Game.current);
            Save ();
            Load ();
        }
    }
}

This is the menu script that handles the menus and allows the player to access their profiles:

using UnityEngine;
using System.Collections;

public class MainMenu : MonoBehaviour
{

    public enum Menu
    {
        MainMenu,
        NewGame,
        Continue,
        Delete
    }

    public Menu currentMenu;

    void OnGUI ()
    {

        GUILayout.BeginArea(new Rect(0,0,Screen.width, Screen.height));
        GUILayout.BeginHorizontal();
        GUILayout.FlexibleSpace();
        GUILayout.BeginVertical();
        GUILayout.FlexibleSpace();

        if (currentMenu == Menu.MainMenu)
        {

            GUILayout.Box ("Anti-Grav");
            GUILayout.Space (10);

            if (GUILayout.Button ("New Game"))
            {
                Game.current = new Game ();
                currentMenu = Menu.NewGame;
            }

                if (GUILayout.Button ("Load Game"))
                {
                    SaveLoad.Load ();
                    currentMenu = Menu.Continue;
                }

                if (GUILayout.Button ("Delete Profile"))
                {
                    currentMenu = Menu.Delete;
                }

                if (GUILayout.Button ("Quit"))
                {
                    Application.Quit ();
                }
            }
            else if (currentMenu == Menu.NewGame)
            {
                GUILayout.Box ("Profile Name:");
                GUILayout.Space (10);

                GUILayout.Label ("Profile");
                Game.current.Player.Name = GUILayout.TextField (Game.current.Player.Name, 20);

                if (GUILayout.Button ("Save"))
                {
                    //Save the current Game as a new saved Game
                    SaveLoad.Create ();
                    //Move on to game...
                    Application.LoadLevel ("Test");
                }

                GUILayout.Space (10);
                if (GUILayout.Button ("Cancel"))
                {
                    currentMenu = Menu.MainMenu;
                }

                }
                else if (currentMenu == Menu.Continue)
                {
                    GUILayout.Box ("Select Save File");
                    GUILayout.Space (10);
           
                    foreach (Game g in SaveLoad.savedGames)
                    {
                        if (GUILayout.Button (g.Player.Name))
                        {
                            Game.current = g;
                            //Move on to game...
                            Application.LoadLevel (1);
                            }
                        }

                        GUILayout.Space (10);
                        if (GUILayout.Button ("Cancel"))
                        {
                                currentMenu = Menu.MainMenu;
                        }
           
                }
                else if (currentMenu == Menu.Delete)
                {
                    GUILayout.Box ("Select Profile to Delete");
                    GUILayout.Space (10);
           
                    foreach (Game g in SaveLoad.savedGames)
                    {
                        if (GUILayout.Button (g.Player.Name))
                        {
                            Game.current = g;
                            SaveLoad.Delete();
                            return;
                        }
                    }
                GUILayout.Space (10);
                if (GUILayout.Button ("Cancel"))
                {
                    currentMenu = Menu.MainMenu;
                }
            }

        GUILayout.FlexibleSpace();
        GUILayout.EndVertical();
        GUILayout.FlexibleSpace();
        GUILayout.EndHorizontal();
        GUILayout.EndArea();

    }
}

And this is a simple savepoint script, it just calls the SaveLoad Script but I might as well include it:

using UnityEngine;
using System.Collections;

public class SavePoint : MonoBehaviour
{

    public float groundRadius = 1f;
   
    public bool PlayerContact = false;
    public LayerMask whatIsPlayer;
   

    // Update is called once per frame
    void Update ()
    {
        PlayerContact = Physics2D.OverlapCircle (transform.position, groundRadius, whatIsPlayer);
       
        if (PlayerContact)
        {
            Game.current.Player.LoadPositionX = transform.position.x;
            Game.current.Player.LoadPositionY = transform.position.y;
            Game.current.Player.LoadPositionZ = transform.position.z;
            Game.current.Player.RoomNumber = GameObject.FindWithTag("Player").gameObject.GetComponent<RoomTracker>().roomNumber;
            SaveLoad.Save();
        }
    }
}

Cheers for any and all help!