How to "Lock" scriptable objects from losing their [SerializeField] values
Hello all,
I have several "parent" scriptable objects whose serialized field i occasionally assign via an editor tool. The type of the assigned value is also a scriptable object, and has only one instance since the type holds no state -it only has a void method. But every time i instantiate a "parent" scriptable object, and assign its serialized (scriptable object type) field with that one scriptable object instance, the earlier initialized "parent" then loses its assigned field value. Could this be because i'm trying to assign the same scriptable object instance to several instance fields? I thought this is one of the reasons for why you might want to use scriptable objects? Please help.
Here's a representation of the instantiation and initialization code:
public static void Setup(string parentID)
{
string parentName = _parentNamePrefix + parentID + ".asset";
string[] parentNames = Directory.GetFiles(_pathOfParents, parentName);
bool parentExists = parentNames.Length > 0;
if ( parentExists )
{
Debug.Log($"Parent Existed, Did Not Instantiate!");
return;
}
string finalParentName = _pathOfParents + _parentNamePrefix + ".asset";
_parentScriptableObj = CreateInstance<ParentScriptableObj>();
AssetDatabase.CreateAsset(_parentScriptableObj, finalParentName);
if ( !_parentScriptableObj ) return;
string scriptableMethodName = _pathOfValue + ".asset";
ScriptableMethod scriptableMethod = AssetDatabase.LoadAssetAtPath<ScriptableMethod>(scriptableMethodName);
if ( !scriptableMethod )
{
Debug.Log($"Could not retrieve scriptable method!");
return;
}
_parentScriptableObj.Method = scriptableMethod;
}
Your answer
Follow this Question
Related Questions
ScriptableObject not saving to Asset properly 0 Answers
LoadAssetAtPath doesn't seem to work for ScriptableObject asset in AssetPostProcessor 1 Answer
Get asset file from scriptableobject (Nested Scriptableobjects) 2 Answers
How can I get a sub asset? 1 Answer
ScriptableObject Instances Referencing One Set of Prefabs - Changing One SO Changes The same prefab 1 Answer