How I got here: I have been working on a tool that uses scriptable objects which have child scriptable objects. The system works really well, however when I rename the SO the parent/child relationship breaks in Unity.
I have managed to get this down to a very simple case study now.
Using the code below, you create a Container SO asset. Then using the MyMenu you create the Data which is a child to the Container. All this works as expected and the relationship in the Project is:
Container -> Data
However, once you rename the Container SO to something else, Unity reorders the Parent Child so that the Data is now the Parent and Container is the Child:
Data -> Container
Below is the code that should duplicate this issues (Unity 2020.1.17f1 - Also had same results on 2020.1.8)
Container.cs
using UnityEngine;
[CreateAssetMenu]
public class Container : ScriptableObject
{
}
Data.cs
using UnityEngine;
public class Data : ScriptableObject
{
}
CustomEditor.cs
using UnityEditor;
using UnityEngine;
public class CustomEditor : Editor
{
[MenuItem("MyMenu/Create Data for Container")]
static void CreateDataForContainer()
{
Container container = Selection.activeObject as Container;
if (container != null)
{
Data data = ScriptableObject.CreateInstance(typeof(Data)) as Data;
data.name = data.GetType().Name;
if (!string.IsNullOrEmpty(AssetDatabase.GetAssetPath(container)))
{
AssetDatabase.AddObjectToAsset(data, container);
}
AssetDatabase.SaveAssets();
}
}
}
NOTES
- At first I thought this was an issue with namespaces but not the case
- Thought this could be an assembly issue but not the case now with basic classes
- Close/Open project results are same
- ASSET file is identical after rename
If you experienced this I'd love to know what I'm doing wrong here, my tool is almost done and this is stopping me from pushing it out. Thanks!