Jump to content

April 2017 »

S M T W T F S
      1
2345678
9101112131415
16171819202122
232425 26 272829
30      

Recent Entries

Recent Comments

- - - - -

Script execution order



4: Adsense

Just had my first lesson in script execution order in Unity.

I have a MyMethod() within Update() that looks to a static integer set by another script. MyMethod() was not functioning because upon starting the game, the method would break due to the static int being set to 0. The static in question is dynamic but it's range is not allowed to fall to 0 per it's script. How on earth could it be 0?? Then I decided to debug.log the static in Update() and upon Start(). I found that the static was 0 in the first frame, then it would set correctly from the script that initializes it every frame after. However, just one frame of 0 was all it took to break my method within Update(). I'm sure I could get around this other ways but I found the quickest way to fix this issue was to use the script execution order in Unity. I set the script with MyMethod() to run last, allowing Unity time to initialize the static integer. I won't tell you how long it took me to diagnose this, but I'm writing this post out of relief. Anyone else encountered this?

Mar 15 2017 03:45 PM

A static integer in another script seems like a bit of a code smell.  What do you use it for?

Mar 20 2017 06:33 AM

I won't deny that I'm cooking a steamy plate of spaghetti.

Note: GameDev.net moderates comments.