The waiting game
The reason why we're declaring these variables at the top of the script, and defining them (giving them their values) further down in the Start
function, is partly stylistic, and partly practical. There's a potential problem here: because Unity is superfast at calculating the Screen.width
property, it might do so before the game screen has finished resizing and initializing, returning an incorrect value. By moving the variable definition to the Start
function, we're giving Unity a few CPU cycles to make itself decent before we go barging in demanding screen properties.
Defining halfButtonW
at the top of the class won't break anything, but some programmers would prefer to see the definition separated from the declaration.
Get Unity 4.x Game Development by Example Beginner's Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.