Script execution order

posted in VirtualRN's Blog
Published March 15, 2017
Advertisement

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?

2 likes 2 comments

Comments

gamedevnet_signin_busted

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

March 15, 2017 09:45 PM
VirtualRN

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

March 20, 2017 12:33 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement