I have this script that works flawlessly while testing on my computer, but doesn’t work on my Pixel 4. Does anybody have any idea why this won’t work or how to fix it?
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public static class SaveSystem
{
static readonly string fileType = ".json";
static readonly BinaryFormatter formatter = new();
public static void SaveTeam (Team team, string folder)
{
var fileName = Application.persistentDataPath + "/" + folder + "/" + team.index + fileType;
FileStream stream = new (fileName, FileMode.Create);
TeamData teamData = TeamData.SaveTeam(team);
formatter.Serialize(stream, teamData);
stream.Close();
Debug.Log("Saved " + team.Name + " Team to " + fileName);
}
public static void LoadTeam (Team team, string folder)
{
var fileName = Application.persistentDataPath + "/" + folder + "/" + team.index + fileType;
if (File.Exists(fileName))
{
FileStream stream = new(fileName, FileMode.Open);
TeamData data = formatter.Deserialize(stream) as TeamData;
stream.Close();
data.LoadTeam(team);
Debug.Log("Loaded "+team.Name+" Team from "+fileName);
}
else
{
Debug.Log("File not found in " + fileName);
}
}
public static float[] ColorToArray (Color color)
{
float[] array = new float[3];
array[0] = color.r;
array[1] = color.g;
array[2] = color.b;
return array;
}
public static Color ArrayToColor(float[] array)
{
Color color = Color.white;
color.r = array[0];
color.g = array[1];
color.b = array[2];
return color;
}
}
[System.Serializable]
public class TeamData
{
public string Name;
public float[] color;
public static TeamData SaveTeam (Team team)
{
TeamData data = new();
data.Name = team.Name;
data.color = SaveSystem.ColorToArray(team.primaryColor);
return data;
}
public void LoadTeam (Team team)
{
team.Name = Name;
team.primaryColor = SaveSystem.ArrayToColor(color);
}
}