Unit thinks its GridPosition is 0,0 at start of the game

My Problem:
After making the GridSystemVisual script only update when needed (instead of on every frame), I noticed when starting the game the selected unit always displayed as if it were at GridPosition 0,0.

image

I’m pretty sure my events are set up correctly because everything works after I’ve taken an action or changed my selection. This grid will update as expected. It only messes up at the beginning of the game (even with UpdateGridVisual() being called in the Start function).

My Fix:
There were no problems in my GetValidActionGridPositionList or IsValidGridPosition functions. I found that if I set the Unit’s grid position in the Awake function instead of the Start function, it works.
image

But,
Now I’m getting these errors (line 27 is where I set the GridPosition in the Awake function):
image

From these errors, I assume this is not the correct way to fix this problem. Any ideas on how this can be fixed?

1 Like

Making sure the Unit script runs before Default Time in Project Settings works, but I’m still curious if there are any other ways to prevent this.

4 Likes

The tricky part about the Awake() function is that the LevelGrid may not have been initialized yet.

Try setting the GridPosition in the Start() function instead, which will happen after all of our needed classes will have finished their initialization.

Privacy & Terms