(Successor to https://mossgoblin.com/2020/02/18/egg-and-chicken-in-unity/)
I got this problem again, when different component scripts compete for precedence. Always something to think about when you have multiple entities (not the technical term from dots).
In this case I was integrating the successfully prototyped hud/radar system into the main scene and I got the following situation:
The MapController (the main conductor in the scene) gets a list of points of interest (POI) to be displayed on the huds. It send them to the HudController.
It also positions the players and sends their transforms to the HudController again.
The HudController decides which of the POI are visible for each player and creates two separate POI lists and sends them to the player huds, along with the respective player transforms.
Then each hud takes care of initializing the compass of the player and displays the POI list it has.
Who Starts() first. Madness and definitely not the proper way.
We leave aside the fact that the two controllers and both the huds are component scripts and not simple C# objects. Quite the anguish, deciding it should be so. I would not have the chaining problem if I used pure C# classes for those, but building and setting up the scene turned out to be much much easier with properly exposed component scripts.
So… one Start() to rule them all! Here’s how it ended up looking:
MapController
+Start()
- PrepPlayers()
- Send players to HudController
- PrepGeneralPOIList()
- Send generalPOIList to HudController
- HudController.Run()
HudController
+Run()
- Send players to each player Hud
- Split generalPOIList into two playerPOILists
- Send one playerPOIList to each Hud
- for each Hud: Hud.Run()
Hud
+Run()
I feel weird having component scripts that have logic, but don’t have Start() hook, but.. it seems to work. I am pretty sure that’s not optimal and there are better ways to organize the action flow. But it will have to wait for some other project.