Serialization data being persisted between instances of a GameObject

I’m working on an editor script right now and running into a serialization issue. The Monobehivor script that it edits has a serialized class in it that inherits ScriptableObject.

If I modify (via my editor script) the data in that SerializedObject, Then delete the instance of the MonoBehavior from the hierarchy and create a new instance (done via a menu static class that creates a GameObject and adds the required script/Meshfilter/Renderer etc) the data that was being serialized in that first (now deleted) instance is now associated with the newly created instance.

Any ideas why this is happening and how to prevent it?

For reference. I have combed through the answers/posts on the following pages (and many others) with much success in solving other previous issues, But Have found no answers as to why this particular problem is happening.

Null Object after serialization

a guide to editor script serialization…

Serialization best practices megapost

I fully acknowledge that I may have missed something. So if nobody has any ideas based on my description, I’ll edit this post to include the code I’m working with. Technically the class tree that I’m trying to serialize is a bit more complicated and includes a List of yet more serialized objects.

I should note, the serialization IS working the way I expect it to with the exception of this odd persistence problem.

The reason it is persisting is because Scriptable Objects are only instantiated once. If you create a Scriptable Object at runtime, the instance is saved to the scene data as long as it’s being referenced, otherwise it gets collected by the Garbage Collector. If you create an Asset of the Scriptable Object, the instance is stored on it. If you then drag and drop that asset into the inspector on a reference of that Scriptable Object, it will be a reference of that Scriptable Object. This holds true with any class and how references are made. The only time this breaks is upon serialization and deserialization. When you serialize an instance, it saves all of the data, and upon deserialization, you now have a “clone” of the original reference. However, Unity has it setup to only serialize the Instance ID of Scriptable Objects and upon deserialization, Unity looks up the Instance ID of all of the Scriptable Object instances and reestablishes the reference. I hope that better explains the process.