Execute Unit.cs before GridSystemVisual.cs

GridSystemVisual.Start() calls GridSystemVisual.UpdateGridVisual() which calls MoveAction.GetValidActionGridPositionList() which calls Unit.GetGridPosition() which returns Unit.gridPosition.

However Unit.gridPosition is initialized in Unit.Start()! So, when the game starts, it randomly bugs if Unit.Start() of the default selected unit is executed after GridSystemVisual.Start().

To fix it, I changed the script execution order for Unit.cs:

1 Like

Yeah that is one of those issues that depends on how Unity decides the order of the scripts. When I was making the course Unity ordered them “correctly” so I didn’t see that issue, only noticed when I duplicated the project to test some things and it worked differently.

Instead of making the Unit run before the default time I would make the Visual script run after the default time, usually you want visuals to run at the end. But making the Unit run first also works.

1 Like

Runnings Visual scripts after the default time sounds like a good practice. Thanks for the advice!

1 Like

It would be helpful for me if @CodeMonkey included a screen shot of the FINAL script execution order (from after the final lesson)

My default action does not show the grid, so i am currently setting the grid visual in a coroutine.

  private void Start()
    {
        // late start because the grid visual was not displaying
        // related: https://community.gamedev.tv/t/race-condition-with-the-unit-referenced-in-the-editor-on-unitactionsystem/205735
        StartCoroutine(LateStart(0.1f));
//... rest of start code (minus UpdateGridVisual)
}
 IEnumerator LateStart(float waitTime)
    {
        yield return new WaitForSeconds(waitTime);

        UpdateGridVisual();
    }

Singletons and events are hard to reason about.

I showed it every time I modified it but the default order is semi-random, so that is why I did not see the issue in the original post, because in my case my Unity ordered them “correctly” but in the original poster’s case Unity ordered them differently.
If you find issues with the order, make the visuals run after the default.
Using a coroutine also works but I prefer changing the order.

1 Like

Privacy & Terms