Unit script didnt create baseActionArray

Hi all,

I was reading the other questions here in the course but I had a different problem. In my case, selectedUnit is not null but when I do the foreach inside de UnitActionSystemUI to check what are the actions my unit has, I saw that I got a null error there bcs the array wasnt created. My fix was to change the order of execution for the Unit script but idk if you know a better solution.

Thanks!

Welcome to the community @andreabefuen

Do you create the array inside the unit’s Awake method? If so, I guess your fix will be fine. If not, try moving it there and see if it makes a difference

A word of warning on changing script execution order, which we do a lot of in this course. It’s important whenever we change the execution order, to make sure that we don’t wind up creating new issues. For example, you might move script A to execute before script B, and B to execute before script C but later have an issue where script A actually relies on something in script C…

In many cases these issues can be resolved with a few simple rules:

  • Awake() => Cache references (this would include building your array of BaseActions). Do not act on any external reference here, except that you may subscribe to events here. Initialize variables that do not depend on external references here.
  • OnEnable/OnDisable() => Subscribe and unsubscribe to events here, unless you subscribed in Awake()
  • Start(), Update() => You should now be free to act on external references.

This isn’t to say never fiddle with the execution order (as I said, we do it in a couple of places in this course, and Hugo was mindful of the risks when he set them), but always be mindful of what depends on what so you don’t wind up just passing the race condition around to the next script.

2 Likes

I almost always avoid changing the execution order. I may do it in a game jam when I couldn’t be bothered to figure out how to prevent the issues that is prompting me to do it, and I don’t usually have the time in a game jam to be bothered in the first place. But if I can avoid it, I do. It is a last resort kind of fix

1 Like

Privacy & Terms