What "really" happens when Prefab connection is broken?

ok this is another of my quite complicated questions, hope to find some answers this time:

Using a destructor in a simple c# class (not monobehaviour) I suddenly introduced the following error:

“ArgumentException: CompareBaseObjects can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.”

However this questions is not really focused on this problem, even though this error helped to notice what I will try to explain:

I had a prefab with a gameobject using a component which was using on its turn an object with this destructor.

Whatever the destructor problem was (not sure if it was because of the destructor itself or because of the code called inside the destructor, not really important), Unity was spitting the mentioned error. So far so good (just saying).

Then I started to notice that this error was generated even in scenes where this prefab was not used at all, the gameobject with the aforementioned component was not present or created.

This is what drove me crazy. After a while I realized that the scene was actually using a GameObject generated from the original prefab (the guilty one), but the link was broken and most of the gameobjects deleted, included the one using the object with the destructor!

In practice the copied/unlinked prefab was eventually transformed into something completely different, but still referencing somehow not only the original dependencies, but even the original code! Crazy.

in bullet points:

  • Unity started to spawn an error after a class with a destructor has been introduced.
  • The class was used by a component inside a gameobject of a prefab
  • Another scene, that was not apparently using the prefab, was generating the same error of the point 1
  • It has been found out that the same scene was actually using a hierarchy of gameobjects generated by the original prefab, but after many iterations it was completely changed and was not using the same gameobjects anymore. Of course the hierarchy was originally created dragging and dropping the prefab in the scene via editor and breaking the link (no code involved)
  • this allegedly means that even if a prefab link is broken and the original gameobjects completely erased, somehow the original code is still used?!

What do you think it is going on?
If I copy a Prefab in my scene via editor and break the connection, is the code of the components of the gameobjects included in the original prefab somehow executed even if the gameobjects from the cloned prefab are deleted?

(I re-edited the post to be clearer about the problem)

Ok this is going to be very complicated without the code or the exact circumstances…

I also don’t see th relation between your destructor-error and the prefab. What dependencies are you talking about? When you clone a gameobject (no matter if it’s a prefab[off-scene] or any other gameobject[in-scene]) all serializable values get copied. If you had a reference to for example a Mesh instance, the clone would point the same Mesh instance.

Ok, now face the destructor problem. A C# destructor is called by the garbage collector when the object is removed from memory. If you cut all references to a C# object the GC will collect the object, but not immediately. The destructor should only be used for cleanup of the object itself.

For example if you have a MonoBehaviour and use Destroy on it, the script instance will be “marked” as destroyed. The native code part of it will be destroyed after the current frame (since Destroy is delayed until the end of the current frame). However the managed code part of the script is still there until it is collected by the GC.

Unity provides a lot callbacks for almost every case.

That said i don’t think anyone could give you much more hints without some code or more details.