I have a MonoBehaviour class that has data in it like:
public class C : MonoBehavior
{
public GameObject referenceToOtherPrefab;
public A[] myAList;
}
// C# class "A"
public class A : ScriptableObject
{
public MyEnum someEnumVal;
public GameObject[] otherPrefabs;
public C myRefToOwnerC;
}
Anytime I create a GameObject prefab with a C component on it I can edit the data just fine with a custom editor class. The Editor class for C manages allocating the A instances with ScriptableObject.CreateInstance() and allocating the arrays.
Yet whenever I run the game or close and re-open the editor these PREFAB objects get their data wiped. Not the scene versions the prefab versions just lose their data.
Whats weird is that even the referenceToOtherPrefab member gets lost and that is just a simple GameObject reference to a prefab that I have used successfully in many other examples. Its as if there is something simple I am overlooking that is preventing the C class from being serialized in any way shape or form.
To clarify, the data that is being reset is not the ones within any scene instance or run-time instance. It is the prefab instances.
ScriptableObject is not serialized in the current scene. They have to be stored in an asset manually (AssetDatabase). In your case it seems that it would make more sense to use just a serializable cursom class. It’s not the MonoBehaviour that is losing it’s data, it’s the ScriptableObject which simply doesn’t exist after a restart if you haven’t saved it somewhere.
We had similar question a couple of times. See this one for example:
Unless you’re interested in writing an editor for your class you should stay away from ScriptableObject at least when you want it to be serialized.
Hello, I had the same problem with a list of Monobehivour classes that was filled in edit mode.
There was no problem with it until I prefabbed the object that the script was on.
After I prefabbed it when I was going to play mode the data in the list was completely lost. And it fixed when unpacked the prefab. So maybe it’s not only for scriptable objects or play mode. Hope this helps.
This is simple as if you would follow basics of Unity you would know that anything changed during actual Play Scene is not saved. It is allowed to change just for test purposes but after you stop game execution all data will be reseted to its initial values.
if you want the values to be remembered you have to assign them in the script or make some kind of another script to assign them dynamically.