Hi guys, it’s like 2 days that i’m struggeling to resolve this problem and i coudn’t find any solution online.
My game is similar to Subway surfer. You have a car and you can swipe to avoid obstacles and take coins. In my game i have multiple cars that the user can unlock with coins. Every car have it’s own info. In this info i wanna add the prefab gameobject for know the model to display.
For accomplish this i created a class that initialize the informations that i called GameData.
/// <summary>
/// Class with data to save
/// </summary>
[System.Serializable]
public class GameData
{
/// <summary>
/// Coin count
/// </summary>
public int Coins;
/// <summary>
/// Cars info
/// </summary>
public List<CarData> Cars;
public GameData()
{
// Init data with default values
this.Coins = 0;
this.Cars = new List<CarData>
{
// Car 1 Info
new CarData
{
Owned = true,
Type = CarType.Jeep_Car,
Price = 0,
BulletStartPosition = new Vector3(0f,1f,0f),
UIScale = new Vector3(20f,20f,20f),
UIPosition = new Vector3(0f,0f,0f),
UIRotation = Quaternion.Euler(10f, -130f, 0f),
//Prefab = CarPrefabs[0],
Active = true,
Description = "This is the base car",
Upgrades = //... Upgrades info
},
// Car 2 Info
new CarData
{
Owned = false,
Type = CarType.Jeep_OffRoad,
Price = 1000,
BulletStartPosition = new Vector3(0f,1f,0f),
UIScale = new Vector3(20f,20f,20f),
UIPosition = new Vector3(0f,0f,0f),
UIRotation = Quaternion.Euler(10f, -130f, 0f),
//Prefab = CarPrefabs[1],
Active = false,
Description = "This is the second car",
Perks = //... Perks info
Upgrades = //... Upgrades info
}
//... Every car info
};
}
}
This class is used by another class that save and load the data.
public static class SaveDataManager
{
/// <summary>
/// Current data of the user
/// </summary>
public static GameData CurrentData = new GameData();
/// <summary>
/// Data file dir
/// </summary>
public static string DataDir = "/Saves/";
/// <summary>
/// Data file name
/// </summary>
public static string DataFileName = "Save.sav";
/// <summary>
/// Save the game data
/// </summary>
/// <returns></returns>
public static bool SaveGameData()
{
string dir = Application.persistentDataPath + DataDir;
// Create dir
if(!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
// Convert class to json
string json = JsonUtility.ToJson(CurrentData, true);
// Write data in the file
File.WriteAllText(dir + DataFileName, json);
return true;
}
/// <summary>
/// Load the game data
/// </summary>
/// <returns></returns>
public static bool LoadGameData()
{
string fullPath = Application.persistentDataPath + DataDir + DataFileName;
// Check if the save file exist
if(!File.Exists(fullPath))
{
Debug.Log("Saves doesn't exist, init it");
// Init the save data
SaveGameData();
}
// Read file
string json = File.ReadAllText(fullPath);
// Convert json to class and store it in the current game data
CurrentData = JsonUtility.FromJson<GameData>(json);
return true;
}
}
My problem is that i don’t know how to set the Prefab variable in the game data as it need to be NonSerializable because i don’t need to save the gameobject on the file but i need it in the inspector for assign the Car1, Car2, Car3… prefabs. At the start i used the Resources folder and load the prefabs by path but searching online i’ve found that it isn’t a good solution. So how can i pass the gameobjects by reference to the GameData class?
Thank you for your help and time
P.S. I made a mistake. I set the thread as bug instead of question. Sorry