Hi, in an hypothetic scenario where:
I have A.cs and B.cs (defining A and B classes respectively) where A depends on B and B on A.
A.cs and B.cs are loaded via addressables and they may be available or not and in any order.
If A.cs is loaded but not B.cs, there would be any issue?
Is there a way or good practice to preload your scripts in Unity in a given order?
Any advice on preventing circular references in Unity?
Another example from a real situation:
I have ItemCategories and a FactoryItemCategories.
FactoryItemCategories needs to know (depends on) ItemCategories to create it, isn’t it?
Now let’s say I have Item which has a category so:
FactoryItem needs to know Item to create it, and Item needs to know FactoryItemCategories to reference its category.
You can do circular references all day long. It sounds like what you’re trying to deal with is order of initialization.
These can be tricky, but the go-to way will always be to get some 3x5 cards and write the individual steps you need on them, and then you can reorganize them so that results produced in one step are clearly done before the next step.
If you’re concerned about scripting call order, this is Unity’s timing diagram:
If you want to explicitly control beyond the granularity provided by the above diagram, do it with explicit init calls. I like making a factory type pattern such as this one:
Factory Pattern in lieu of AddComponent (for timing and dependency correctness):
It has many benefits over just throwing a pile of stuff in your scene.