Hello,
I’ve got a really strange problem on which I spent a lot of time trying to solve but that I was unable to.
I’ve read through the threads but it seems as no one had the same problem yet. If not, sorry and please post the link for me
Also I watched this video (Serialization in-depth with Tim Cooper) but it didn’t help me.
So, the problem is as follows:
I’ve a class “PlayerData” that derives from ScriptableObject and I created an .asset file with the script “MakePlayerData”. That all worked fine so far. But when I use it in the game, it only functions if I select the asset and inspect it at runtime. If I don’t do this, the .asset file will loose all it’s content and will be reset.
I use the .asset file as follows:
To get access in an arbitrary class, I just defined a
public PlayerData data;
and referenced it through drag and drop.
To mention: I do an
Application.LoadLevel(int);
but before I update all the information to the referenced .asset file (by accessing the variables directly).
Also, in the exported version it never functions.
Here is the code I talked about:
using UnityEngine;
[System.Serializable]
public class PlayerData : ScriptableObject
{
// Stats
public int playerHighScore = 0;
public int lastPlayerScore = 0;
public int diamondScore = 0;
public int emeraldScore = 0;
public int rubyScore = 0;
public int goldScore = 0;
public int silverScore = 0;
public int bronceScore = 0;
public int beerScore = 0;
public int coveredMeters = 0;
public int lastCoveredMeters = 0;
public int playerDeaths;
public int commerceRuneScore = 0;
public int protectionRuneScore = 0;
public int metalRuneScore = 0;
public int thirstRuneScore = 0;
public int septaRuneScore = 0;
// Runes properties
public int commerceRuneDuration = 10;
public int protectRuneDuration = 20;
public int metalRuneDuration = 10;
public int thirstRuneDuration = 10;
public int septaRuneSpawnrate = 20;
// ElementGeneratorScript's coinProbabilities
public int[] coinProbabilities = new int[5]{0, 0, 0, 0, 0};
public void setCoinProbabilities(int[] probabilities)
{
for(int i = 0; i < coinProbabilities.Length; i++)
coinProbabilities[i] = probabilities[i];
}
// Shop
// BasicUpgrades
public int coinsBUElementItemState = 0;
public int commerceBUElementItemState = 0;
public int protectBUElementItemState = 0;
public int metalBUElementItemState = 0;
public int thirstBUElementItemState = 0;
public int septaBUElementItemState = 0;
}
#if UNITY_EDITOR
using UnityEngine;
using System.Collections;
using UnityEditor;
public class MakePlayerData
{
[MenuItem("Assets/Create/Player Data")]
public static void createPlayerDataAsset()
{
PlayerData asset = ScriptableObject.CreateInstance<PlayerData>();
AssetDatabase.CreateAsset(asset, "Assets/PlayerData.asset");
AssetDatabase.SaveAssets();
EditorUtility.FocusProjectWindow();
Selection.activeObject = asset;
}
}
#endif
I really hope you can help me.
Thank you!
Cheers,
Arawan