3

When I launch my game in game editor or on the phone - it glitches for the 2-3 seconds and calls the function OnApplicationPause. But when I close application/pause in editor it doesn't calls. That's bad for my project so how to fix it? There's function code :

void OnApplicationPause()
{
    DateTime time = DateTime.Now;
    SaveVariablesInClass();
    PlayerPrefs.SetString("SV", JsonUtility.ToJson(save)); //saves special class
}

void SaveVariablesInClass()
{
    save.Bananas = bananas;
    save.GoldBananas = goldBananas;

    save.TwoBananasSpawnChance = twoBananasSpawnChance;
    save.HigherBananaLevelChance = higherBananaLevelChance;
    save.SimpleBananaStormChance = simpleBananaStormChance;

    save.GoldBananaChance = goldBananaChance;
    save.TwoGoldBananaChance = twoGoldBananaChance;

    save.offlineEfficiency = offlineEfficiency;
    save.offlineProductionTime = offlineProductionTime;

    save.goldCoefficient = goldCoefficient;

    save.BonusLevels = bonusLevels;
    save.GoldBonusLevels = goldBonusLevels;
    save.SimpleBonusLevelPrices = simpleBonusLevelPrices;
    save.GoldBonusLevelPrices = goldBonusLevelPrices;
    save.simpleMaxLevels = simpleMaxLevels;
    save.goldMaxLevels = goldMaxLevels;

    save.time = time.ToString();

    save.CurrentSimpleBonus1 = currentSimpleBonus1;
    save.LaunchedGame = launchedGame;
}
7
  • Could you please add your code? Also please decide for one tag .. your code is either c# or javascript but probably not both. What do you mean by it slows down a bit? When you are launching the App how can it slow down? Also have a look at OnApplicationPause
    – derHugo
    Jun 26, 2019 at 15:12
  • I have edited the question
    – JediMan
    Jun 26, 2019 at 15:28
  • did you try giving OnApplicationPause a bool parameter and skip if it is false?
    – derHugo
    Jun 26, 2019 at 15:33
  • I tried it now, but then I can't save my progress. Some time ago this did not happen.
    – JediMan
    Jun 26, 2019 at 15:38
  • Yes it should still be done if the bool value is true. What is SaveVariablesInClass(); doing? just trying to see where the lag could be caused
    – derHugo
    Jun 26, 2019 at 15:39

1 Answer 1

1

You are missing the bool parameter in the method. It should be OnApplicationPause(bool pauseStatus). OnApplicationPause is called on each GameObject after Awake. So your code should be:

void OnApplicationPause(bool pauseStatus)
{
    if (pauseStatus)
    {
       DateTime time = DateTime.Now;
       SaveVariablesInClass();
       PlayerPrefs.SetString("SV", JsonUtility.ToJson(save)); //saves special class
    }
}

The lag you show in comments is caused by rendering not script.

3
  • I tried it again and my method does not work anyway. Neither during a pause nor when leaving the game.
    – JediMan
    Jun 27, 2019 at 11:47
  • Thanks! It started working when I tested it on the Android device!
    – JediMan
    Jun 27, 2019 at 12:36
  • No problem. Also PlayerPrefs are not the best for saving all the game data. Data saved in PlayerPrefs is easy to change by a user and should be used mainly for things like game settings.
    – Dave
    Jun 27, 2019 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.