I have a GameObject in the scene. This GameObject has a script on it that has a public member variable that takes a GameObject in its slot. I fill this slot with a prefab defined in my Assets folder.
The scripts on the prefab that fill the slot behave strangely in the following ways:
Their Start and Awake methods do not run, but their constructor does.
A generic list, IList private member variable in the script that is never initialized in script, remains null. However, a List private member variable that is never initialized in script is automagically initialized at some point during Unity’s setup.
I can see how item 1) is working as intended maybe, but WTF is up with item 2) ?
Is filling slots with prefabs in this way okay? Or is that breaking some kind of Unity best practice? How should I do it to avoid nefarious behaviour such as this?
Attached is a test package featuring the scenario I describe.
It was my understanding that the prefab would be instantiated when put into a slot like this. Apparently my understanding was wrong. As for the constructor, it was really just to see what would happen in this test, I know using constructors in monobehaviours is bad practice.
I know IList is an interface, but why would the List automagically initialize in the first place?
The reason I came across this behaviour is the first place was because this automagical initialization of the List member variable does not occur on IOS builds, only when running the game from inspector.
No not exactly. It is hard to explain any better than I did in the original post. If you open the package attached it will take 30 seconds to see exactly what is happening… but in my opinion the behaviour is indeed very surprising.