Question by
piratecody · Aug 27, 2018 at 11:12 AM ·
androidloadingcrashing
Loading game data crashes on android
My game works in editor but crashes on Android. It appears to break when trying to load from a save file. Essentially, I save a bunch of dictionary values into Lists and then set the values of the dictionaries to the saved list values on load.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class Global {
public static double masterClickCount; //The total number of clicks
public static int adCookies; //
public static Dictionary<string, int> multipliers = new Dictionary<string, int> (); //Key is the clicker/scene, value is the total multiplier
public static Dictionary<string, double> multiplierUpgradePrices = new Dictionary<string, double>(); //Key is the clicker, value is the price of upgrading
public static Dictionary<string, double> clickerPrices = new Dictionary<string, double> (); //Key is the clicker, value is the price to unlock the clicker
public static Dictionary<string,Boolean> unlockedClickers = new Dictionary<string,Boolean>(); //Key is the clicker, value is whether or not its unlocked
public static Dictionary<string, int> autoclickers = new Dictionary<string, int>(); //value is number of autoclickers in a given clicker
public static Dictionary<string, double> autoclickerPrice = new Dictionary<string, double>(); //value is price of autoclickers
public const int STARTING_MULTIPLIER = 1;
public const double STARTING_MULT_PRICE = 50;
public const double STARTING_AUTO_PRICE = 75;
//const values, each one is the name of a scene for the corresponding clicker
public const string COOKIE = "Cookie";
public const string ADCAP = "Adcap";
public const string ANIME = "Anime";
public const string CLICK = "Click";
public const string PLANET = "Planet";
public const int COOKIE_CLICK = 1;
public const int ADCAP_CLICK = 2;
public const string SAVE_FILE = "/clickception.dp";
public List<string> keys;
// Use this for initialization; sets all the initial values
public static void InitStart () {
if (File.Exists (Application.persistentDataPath + SAVE_FILE)) {
LoadSave ();
} else {
//Starting values
masterClickCount = 10000;
adCookies = 0;
//Initial multiplier level
multipliers.Add (COOKIE, STARTING_MULTIPLIER);
multipliers.Add (ADCAP, STARTING_MULTIPLIER);
multipliers.Add (ANIME, STARTING_MULTIPLIER);
multipliers.Add (CLICK, STARTING_MULTIPLIER);
multipliers.Add (PLANET, STARTING_MULTIPLIER);
//Initial multiplier upgrade prices
multiplierUpgradePrices.Add (COOKIE, STARTING_MULT_PRICE);
multiplierUpgradePrices.Add (ADCAP, STARTING_MULT_PRICE);
multiplierUpgradePrices.Add (ANIME, STARTING_MULT_PRICE);
multiplierUpgradePrices.Add (CLICK, STARTING_MULT_PRICE);
multiplierUpgradePrices.Add (PLANET, STARTING_MULT_PRICE);
//clicker unlock prices
clickerPrices.Add(COOKIE, 0);
clickerPrices.Add (ADCAP, 1);
clickerPrices.Add (ANIME, 5);
clickerPrices.Add (CLICK, 1);
clickerPrices.Add (PLANET, 5);
//Initial values for the unlocked clickers. Cookie starts unlocked
unlockedClickers.Add (COOKIE, true);
unlockedClickers.Add (ADCAP, false);
unlockedClickers.Add (ANIME, false);
unlockedClickers.Add (CLICK, false);
unlockedClickers.Add (PLANET, false);
//Initial values for autoclicker prices
autoclickerPrice.Add (COOKIE, STARTING_AUTO_PRICE);
autoclickerPrice.Add (ADCAP, STARTING_AUTO_PRICE);
autoclickerPrice.Add (ANIME, STARTING_AUTO_PRICE);
autoclickerPrice.Add (CLICK, STARTING_AUTO_PRICE);
autoclickerPrice.Add (PLANET, STARTING_AUTO_PRICE);
//Intialize autoclicker dictionary
autoclickers.Add (COOKIE, 0);
autoclickers.Add (ADCAP, 0);
autoclickers.Add (ANIME, 0);
autoclickers.Add (CLICK, 0);
autoclickers.Add (PLANET, 0);
}
Debug.Log (Application.persistentDataPath.ToString ());
}
public static void SaveGame(){
Debug.Log ("Saving");
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + SAVE_FILE);
PlayerData data = new PlayerData ();
data.masterClickCount = masterClickCount;
foreach (var kvp in multipliers) {
data.keys.Add (kvp.Key);
data.multipliers.Add (kvp.Value);
data.multiplierUpgradePrices.Add (multiplierUpgradePrices [kvp.Key]);
data.unlockedClickers.Add (unlockedClickers [kvp.Key]);
data.autoclickers.Add (autoclickers [kvp.Key]);
data.autoclickerPrice.Add (autoclickerPrice [kvp.Key]);
data.clickerPrices.Add (clickerPrices [kvp.Key]);
}
bf.Serialize (file, data);
file.Close ();
data = null;
}
public static void LoadSave(){
Debug.Log ("Loading Save");
if (File.Exists (Application.persistentDataPath + SAVE_FILE)) {
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + SAVE_FILE, FileMode.Open);
PlayerData data = (PlayerData) bf.Deserialize (file);
file.Close ();
masterClickCount = data.masterClickCount;
// foreach (string key in data.keys) {
// int counter = 0;
// multipliers [key] = data.multipliers [counter];
// multiplierUpgradePrices [key] = data.multiplierUpgradePrices [counter];
// unlockedClickers [key] = data.unlockedClickers [counter];
// autoclickers [key] = data.autoclickers [counter];
// autoclickerPrice [key] = data.autoclickerPrice [counter];
// counter++;
// }
for (int i = 0; i < data.keys.Count; i++) {
string key = data.keys [i];
multipliers [key] = data.multipliers [i];
multiplierUpgradePrices [key] = data.multiplierUpgradePrices [i];
unlockedClickers [key] = data.unlockedClickers [i];
autoclickers [key] = data.autoclickers [i];
autoclickerPrice [key] = data.autoclickerPrice [i];
clickerPrices [key] = data.clickerPrices [i];
Debug.Log ("Loading: " + key);
}
}
}
}
[Serializable]
class PlayerData{
public double masterClickCount;
public List<string> keys = new List<string>();
public List<int> multipliers = new List<int> (); //Key is the clicker/scene, value is the total multiplier
public List<double> multiplierUpgradePrices = new List<double>(); //Key is the clicker, value is the price of upgrading
public List<double> clickerPrices = new List<double> (); //Key is the clicker, value is the price to unlock the clicker
public List<Boolean> unlockedClickers = new List<Boolean>(); //Key is the clicker, value is whether or not its unlocked
public List<int> autoclickers = new List<int>(); //value is number of autoclickers in a given clicker
public List<double> autoclickerPrice = new List<double>(); //value is price of autoclickers
}
Comment
you need to use debug on android to get extract error. There is nothing wrong with script above.
https://answers.unity.com/questions/1320966/android-debug-usb.html
$$anonymous$$ight be serialize problem. Wipe data on android might solve it.