Error in script

Hello! I have an incomprehensible situation, I do everything according to the course, to study and to avoid mistakes, I don’t even change the names of your fields, the code is the same and everything works the same as yours, but now I have an error if I don’t use the UpdateGridVisual method, everything good and everything is working up to this point,
i can see the visually available fields and my unit can go to them when it changes position and if i press T again it will update the visual again and show the new available cells, it all works fine, but if i use the update visual method in the Update method, when I get a NullReferenceException when I start the game…
I tried adding this update at startup, it didn’t help, I tried creating a coroutine and calling this method through it at startup, it didn’t help, I tried disabling the second unit, it didn’t help either, I just don’t know what else to try… .my code is 100% the same as yours, i checked it but i got this error…


In that line, either selectedUnit is null or the MoveAction is null.
Add some logs before it to identify which one
Debug.Log(selectedUnit);
Debug.Log(selectedUnit.GetMoveAction());
Maybe you just forgot to drag the default selectedUnit reference in the editor?

yes, that’s right, selectedUnit was null because no one was selected when the game started, I added the “if(selectedUnit != null)” check before the ShowGridPositionList line in the visual update function and now it works correctly. It’s strange why it works for you without checking for null, and I have a similar situation in my UnitActionSystem script,
there, too, in Update I have to check selectedUnit for null otherwise I also get the same error. thanks for the help

It works for me because I dragged the default unit reference in the inspector, so by default it’s never null

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms