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)