1

Hey i want to make a Android Game now my step was Save/Load Data. Now i test it on my PC all goes perfect, The Game creates the Saves folder and saves my bool. Then i put the Apk on my Android device. When the game has to save the bool it stands still. (not a freeze frame but it dont go on).

Here the Code i used to save Data:

Save(It will start the method if you win a level): public void SaveData()

 {
     bool nextlevel = true;

     if (!Directory.Exists("Saves"))
         Directory.CreateDirectory("Saves");

     BinaryFormatter formatter = new BinaryFormatter();
     FileStream saveFile = File.Create("Saves/save.level." + nextScence);

     formatter.Serialize(saveFile, nextlevel);
     saveFile.Close();
 }

Load(I need load in my Level screen to check if the player has played the level):

             try
             {
                 BinaryFormatter formatter = new BinaryFormatter();
                 FileStream saveFile = File.Open("Saves/save.level." + level, FileMode.Open);

                 nextlevel = (bool)formatter.Deserialize(saveFile);

                 saveFile.Close();
             }
             catch (FileNotFoundException e)
             {
                 nextlevel = false;
             }

I hope you can Help me ,thanks for help

I only know this method to Save data if you now a better one and if its go on android to please tell it to me !

Tobias S
  • 35
  • 1
  • 3

1 Answers1

2

You are only checking if player has played a level. Don't make this complicated. Use Unity built in class PlayerPrefs to check if player has played the level.

http://docs.unity3d.com/ScriptReference/PlayerPrefs.html

When player has played level 1, you can create a key called "hasPlayedLevel1" and write 1 to it like below:

PlayerPrefs.SetInt("hasPlayedLevel1", 1); //1 for yes, 0 for no

Later on in the game or when player closes and re-opens the game, you can check if the player has played the level.

    bool hasplayedLevel = false;
    if (PlayerPrefs.GetInt("hasPlayedLevel1") == 1)
    {
        hasplayedLevel = true;
    }
    else
    {
        hasplayedLevel = false;
    }

    if (hasplayedLevel)
    {
        //Players has played this level
    }
    else
    {
        //Players has NOT played this level
    }

You can again mark that the player has NOT played the level by writing 0 to hasPlayedLevel1.

PlayerPrefs.SetInt("hasPlayedLevel1", 0);
Programmer
  • 121,791
  • 22
  • 236
  • 328