- Home /
the most RESOURCE EFFICIENT way of referencing
what is the most RESOURCE EFFICIENT way of referencing some scripts from other scripts?
Is it better to...
... have ONE script with nothing else but PUBLIC STATIC VARIABLES where you can find references of all the other scripts
...or is it better to...
... just reference the components (scripts) when we need them in specific scripts?
... or is there a better way around?
Answer by Tarlius · Jun 06, 2014 at 09:39 AM
I think a better question is why do you care so much about this level of resource efficiency?
I have a feeling you just want to know about the Singleton pattern.
Keeping everything inside one class ("ONE script with nothing else but PUBLIC STATIC VARIABLES) is generally a recipe for disaster. From a code architecture point of view, you will be better off using singletons with a public property than public static variables. Properties will usually compile to the same thing as fields, but the compiler will slap you if you try to do something stupid with them. If you use a Singleton, only the singleton can set its Instance, and if any other class tries to change it the compiler will throw a hissy fit (and save you from yourself)
If you actually care about this level of efficiency, I think if you use statics in other classes you might get more cache misses when looking them up than storing the result locally, but keeping extra copies of the the reference will use more memory. (Like many micro-optimisations, theres a tradeoff)
But seriously, unless we are talking about inner-inner-inner-inner loops inside other multiple nested loops, its probably not going to be something you will ever need to worry about. You would be much better off working on using more efficient algorithms and implementing features your users will actually care about.
IF, however, we are talking about storing a value versus using GameObject.Find or something to look for it, you will probably want to cache the result if you use it regularly. Using Find and GetComponent etc etc is Slow (for a given definition of slow; compared to caching very slow, but not at a level you will usually notice), and I think also feeds the Garbage Collector a little (GC collects are slow in amounts you will actually care about; frame rate stutters)
Your answer
![](https://koobas.hobune.stream/wayback/20220613150920im_/https://answers.unity.com/themes/thub/images/avi.jpg)