- Home /
Global or local variables for use at start function just once
I have a game that use variables only when loading scene.I use all of them for just once. I suppose to think that local variables are running on RAM and the global ones on the harddrive. Am I wrong? Should I create these variables global or local .. which is the best way? Is this effecting apk or ipa size? I want to minimalize the size of my game I make mobile games.
Answer by Teravisor · Feb 16, 2016 at 01:42 PM
Strictly speaking, variables do affect size of apk by several bytes as compiled code changes. Hovewer, even in mobile systems that's nothing, so consider that they don't affect size. Trying to optimize that will bring you to no result.
To minimalize size of game, you should make sure Unity doesn't add any unused libraries inside, compress textures/sounds/etc as much as you can. Next, remove all references to unused assets and make sure there are no unused assets in Resource folder - they are added to package if they are referenced or in Resource folder. Using procedural generation instead of pre-built assets is slow loading-performance-wise, but can reduce size of .apk as well. And that's pretty much it - scripts are very small, trimming them for size of .apk is useless.
About other questions. First you're mixing definitions:
Local variables are variables that are defined inside some scope (usually method/class scope). Most often "local variables" refer to method scope variables while variables in class are called something like "class variables".
Global variables are variables available from anywhere in code. In C# that's singletone variables (=marked with static).
All variable descriptions, default values and how they change are initially stored in binary compiled code(inside .apk/.ipa there is file with it) and when application starts they are all created in RAM so no accesses to disk to write/read variables happen after initial binary loading. Don't mix variables up with assets themselves - assets are loaded from disk only when they are referenced when scene is loaded or there is call to method that loads them like Resources.Load. Local variables are removed from RAM when their scope disappears (method ends/class is destroyed by garbage collector), global variables don't disappear until program ends.
If you're bothered with cluttering RAM with class variables you won't use, add them to some Component that does something with them on Start() and then gets Destroy(...) after they are all used so that RAM is cleared afterwards.
Answer by meat5000 · Feb 16, 2016 at 01:45 PM
If your variable is only going to be used once and then is rendered obsolete it makes sense to declare it within your method so that it becomes destroyed when you are done using it, thus getting back your memory. Declaring Globally will make the variable exist for the lifetime of the script which is not very efficient usage of RAM.
For a few variables its not really important but it is a practice you want to be familiar with, especially if you undertake a large project.
On mobile devices RAM management is REALLY IMPORTANT as mobile OS have a habit of culling processes to recuperate resources. If your Game becomes a strain on the system it may terminate the app.
Dont stress about small projects and micromanagement but DO practice good technique.
Your answer
Follow this Question
Related Questions
Global Variables Refuse to Cooperate 1 Answer
Should I create local copy of global variable? 3 Answers
Editing local variables in another script 2 Answers
Accessing variables from seperate scripts 1 Answer
How to access this variable? 2 Answers