Function "OnApplicationPause" works when the application starts. Android, unity3d

Viewed 1791

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;
}
1 Answers

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.

Related