Hello. On Answers page, I have some problem and cant ask anything when I’m logged in. I dont know if here is right place sorry if it is not.
I am using Binary Formatter for saving the data for game variables. It is saving highscore, coins and loading very well. But when player has 10 coins, I want him to be able to buy some item. It is working on editor perfect. But in android device, it is not working. But again, it is saving and loading coins and highscore. Here is my save code:
public void Save()
{
FileStream file = null;
BinaryFormatter bf = new BinaryFormatter();
file = File.Create(Application.persistentDataPath + "/GameData.dat");
if(data!=null)
{
data.setHighScore(highScore);
data.setCoins(coins);
data.setIsGameStartedFirstTime(isGameStartedFirstTime);
data.setLevels(levels);
data.setMusicOn(isMusicOn);
data.setSelectedLevel(selectedLevel);
bf.Serialize(file,data);
}
}
//Here is my buy code. It is a public method which I am accesing it from UI button's click event.
public void BasketLevel(int index)
{
if(GameController.instance.levels[index]==true)
{
Application.LoadLevel ("BasketLevel");
}
else
{
if(GameController.instance.coins>=10)
{
BuyImages[index-1].SetActive (false);
GameController.instance.levels[index]=true;
GameController.instance.coins-=10;
coinsText.text="" + GameController.instance.coins;
GameController.instance.Save ();
}
}
}
It is perfectly working on Editor, but on device although saving the variables, not doing buying process.
I am using Binary Formatter for saving the data for game variables. It is saving highscore, coins and loading very well. But when player has 10 coins, I want him to be able to buy some item. It is working on editor perfect. But in android device, it is not working. But again, it is saving and loading coins and highscore. Here is my save code:
Saving is saving the variables like high score and coins. Loading is loading the saved variables when app is opened. There is some levels that players can buy with coins. But even if they have enough coins, it is not working. But save and load working which I do them with binary formatter in same class. It is wierd because I have no problem on editor.
There is something I realize. When I try to change the bool variable “isGameFirstStarted” to “true” in the InitializeGameVariables method, it is working. But of course it is not the answer I am searching for. Here is the method which I am calling it in Awake.
void InitializeGameVariables()
{
Load ();
if(data!=null)
{
isGameStartedFirstTime=data.getIsGameStartedFirstTime();
}
else
{
isGameStartedFirstTime=true;
}
//This is variable I am talking about.
if(isGameStartedFirstTime)
{
highScore=0;
coins=0;
selectedLevel=0;
isGameStartedFirstTime=false;
isMusicOn=false;
levels = new bool[7];
levels[0] = true;
for(int i = 1; i<levels.Length;i++)
{
levels[i] = false;
}
data = new GameData();
data.setHighScore(highScore);
data.setCoins(coins);
data.setIsGameStartedFirstTime(isGameStartedFirstTime);
data.setLevels(levels);
data.setMusicOn(isMusicOn);
data.setSelectedLevel(selectedLevel);
Save ();
Load ();
}
else
{
highScore=data.getHighScore();
coins = data.getCoins();
isGameStartedFirstTime = data.getIsGameStartedFirstTime();
levels=data.getLevels();
isMusicOn=data.getMusicOn();
selectedLevel=data.getSelectedLevel();
}
}
One more think, I guess isGameStartedFirstTime variable no coming true. Because when I change the coins variable to some other number, it stays still 0. But if change isGameStartedFirstTime with true it is working. But of course then app thinks at every time it is first time so resetes the variables.