Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PaxForce · Jun 06, 2014 at 08:30 AM · referencepublic static

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tanoshimi · Jun 06, 2014 at 09:40 AM 0
Share

I'd say this is a case of premature optimisation...

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can't Add Scripts, even if they are very first in project and brand new 1 Answer

How do you get your character to look the way you are walking? 1 Answer

Launching local Unity API Reference from MonoDevelop 7 Answers

Using C# how can I setup a "database" script? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges