- Home /
Editor calling a constructor for no apparent reason
This is very strange. I added an ItemInventory class, and made it [System.Serializable]
. Now for some reason, every time Unity compiles my code, the editor tries to instantiate this class which causes a bunch of logs and stuff.
Any ideas why? Look at the log, the first thing I see is the constructor, no hint as to why the constructor is called by the editor in the first place.
Removing the serializable attribute fixes the issue, but this never happened to me before and I have plenty of serializable stuff.
jsons amount: 1
UnityEngine.Debug:Log(Object)
ConfigManager:LoadConfig(String[], Boolean) (at Assets/Scripts/Config/ConfigManager.cs:95)
ConfigManager:.ctor() (at Assets/Scripts/Config/ConfigManager.cs:39)
ConfigUtils:get_ConfigManager() (at Assets/Scripts/Config/ConfigUtils.cs:25)
ConfigUtils:get_GameConfig() (at Assets/Scripts/Config/ConfigUtils.cs:32)
TypeBase:get_TypesConfig() (at Assets/Scripts/Types/TypeBase.cs:16)
TypeBase:GetItemTypes() (at Assets/Scripts/Types/TypeBase.cs:7)
ItemType:get_AllTypes() (at Assets/Scripts/Types/ItemType.cs:7)
ItemInventory:.ctor() (at Assets/Scripts/Items/Inventory.cs:30)
Answer by Tomer-Barkan · Nov 16, 2018 at 05:40 PM
Ended up finding the problem.
I had an ItemInventory field in one of my monobehaviours, and since ItemInventory was defined as Serializable, the editor kept calling it's constructor. This is very weird unity behaviour and I sovled it by marking the field as non-serializable.
Yeah but it was just a reference from a mono to an object that can be null. Why arbitrarily call its constructor? Just leave it null when serializing.
"The serializer does not support null. If it serializes an object, and a field is null, Unity instantiates a new object of that type, and serializes that." on same doc under No support for null for custom classes