0

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!

wbdev
  • 581
  • 1
  • 4
  • 6
  • I don't know much about editor scripting but looking at the documentation for [`AddObjectToAsset`](https://docs.unity3d.com/ScriptReference/AssetDatabase.AddObjectToAsset.html) they recommend re-importing the changed asset to commit the change with `AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(data));` Have you tried that yet? – Ruzihm Dec 23 '20 at 16:06
  • Have you seen [this thread](https://forum.unity.com/threads/whats-happening-with-assetdatabase-addobjecttoasset-cases-1262293-and-1262298.929202/) yet? It might be related. – Ruzihm Dec 23 '20 at 16:46
  • @Ruzihm good idea but I have added this in several placed when its is constructed and the result is the same. Import will force it to save basically (from what I read) and I do an `AssetDatabase.SaveAssets();` at the end which saves and makes the asset ready to use. (If the save is not there the child data will not show up in the editor). – wbdev Dec 23 '20 at 17:00
  • @Ruzihm From looking the thread you posted it seems like they are having issues with it not showing. Right now it will show up but for some reason Unity decides to make the SubAsset the main asset and the Main asset the sub asset. It also changes the name of the subasset to the new name and retains the old name when the main becomes the sub (hope this makes sense). I have also tried `AssetDatabase.AddObjectToAsset(data, AssetDatabase.GetAssetPath(container));` – wbdev Dec 23 '20 at 17:37
  • 1
    @Ruzihm Looks like it was just fixed in a new version, going to test it out. [link](https://issuetracker.unity3d.com/issues/parent-and-child-nested-scriptable-object-assets-switch-places-when-parent-scriptable-object-asset-is-renamed?_ga=2.246476740.1729101191.1608557893-293289097.1608557893) – wbdev Dec 23 '20 at 21:28

1 Answers1

1

Bug fix on Unity's end:

Unity Bug

wbdev
  • 581
  • 1
  • 4
  • 6