Hey,
So im working on a unit movement scene, where units move on a hextile map. It was all working fine, until I duplicated some units. Now the duplicated units are not working properly, because they are calling their start function before my HexTileMapGenerator calls its start function. The other units work fine, only the freshly duplicated units are calling their start function before. I did some print statements and indeed, the units that work correctly are calling their start function after the HexTileMapGenerator calls his.
Whats a suitable solution to this problem? Its the first time I encounter this problem.
Thanks in advance
There’s both Start and Awake with Awake being guaranteed to be called before all Starts. The actual order of any of these calls though is controlled by the script execution order so lots of options there.
I think you have a typo? Awake is called before all Starts?
2 Likes
Indeed. I like to use a lot of additively-loaded scenes, so I tend to code my individual item controllers (including the player controller) so that they are happy to wait around if other stuff isn’t ready yet.
You can make Start() into a coroutine, which is a super-easy way to get this kind of lazy-sloppy startup that is extremely tolerant of late loading and uneven loading.
// typical base framework for all entities
bool ready;
IEnumerator Start ()
{
// don't proceed until the hex tilemap is ready
while( MyHexTileMap.Instance == null)
{
yield return null;
}
transform.position = MyHexTileMap.Instance.GetSpawnPoint();
ready = true;
}
void Update ()
{
if (!ready) return;
// everything else here...
}
void FixedUpdate ()
{
if (!ready) return;
// everything else here...
}
I did indeed, I’ll correct that! Rushed post!
1 Like