Deep Copy of ScriptableObject

Hey, I know a similar topic is floating around in Answers, but it’s some years old and doesn’t work, at least for me…

So I want to copy a scriptableObject WITH its references…
CopySerialized doesn’t work as well as Instantiate… does not…

What am I doing wrong?

        internal static PersonalObject Personalise(this PersonalObject pObject, Object owner)
            string newPath = CreateAssetDirectory(pObject.GetType(), owner);
            newPath = AssetDatabase.GenerateUniqueAssetPath(newPath + "/" + pObject.GetType().Name + ".asset");

            PersonalObject newObject = ScriptableObject.Instantiate(pObject) as PersonalObject;
            AssetDatabase.CreateAsset(newObject, newPath);
            EditorUtility.CopySerialized(pObject, newObject);
            newObject.isPersonalised = true;
            newObject.ownerID = owner.GetInstanceID();
            return newObject;

I shuffled order in any way I could imagine, also used AssetDatabase.CopyAsset, nothing works. Is CopySerialized supposed to use the SerializedObject?


Serialized references will always stay references. The only exception are references to child objects when cloning gameobjects.

ScriptableObjects are assets. Assets can only reference other assets. So if you want a “true” deep copy it would have a quite heavy effect. For example when your scriptable object references other scriptable objects which might reference prefabs. A deep copy would require to also duplicate those “referenced” scriptableobjects as well. However since they are assets they need a new file name. However the referenced prefabs would also need to be duplicated. If the prefabs reference Meshes and materials those need to be duplicated as well. The Material will reference a shader and some textures which would be duplicated as well. I can’t really imagine any situation where you need that.

In most cases you will have only some specific references you want to duplicate. In that case you have to duplicate those manually. Keep in mind that all assets need to be stored in some sort of asset file.

If that doesn’t answer your question you should be more specific about what kind of references you talk about and what you mean by “deep copy”.