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 unity_BjXiCscS4nd7gw · Jan 31, 2021 at 09:24 AM · instancesperfomance

Performance impact of keeping all instances of a game in a single script

Hi there,

I did some research, but couldn't find an answer for it, and it's been bugging me for some time now. Anyway, please forgive the noob question, but here it is: Is more performant to use a script attached to an empty game object, which has all the variable instances the game would use, and then have all the other scripts use the instances from that script?

Let me try to be more clear: I am trying to clean up a code for a game I'm working on using the SOLID principles, and in most scripts that I have, I noticed that in the awake() or start() function, I use instances such as Camera cam = Camera.main, or GameObject player = GameObject.FindGameObjectWithTag("Player"), and so on. So I thought instead of having the instance of cam and player (and other variables I use in the game) repeated in different scripts, why not have them in a single script called ObjectInstances (which has only the awake function), so that other scripts can use the needed variables from this ObjectInstances script. This way, all the game variables would have a single instance in the whole game, and the ObjectInstances script would have an instance once in all the other scripts. Now, that being said, here are my concerns: -Is this way really more performant than having in the game scripts all the instances they need, even if they are repeated (between different scripts)? -Are there some foreseeable problems that you know this way of coding will create? -What about the impact on the memory usage? Any feedback or help would be greatly appreciated, Thank you :)

Comment
Add comment
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

1 Reply

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

Answer by Llama_w_2Ls · Jan 31, 2021 at 10:33 AM

This is definitely more performant due to the fact that you are not using as many GetComponent calls and you are creating less instances of variables that are identical. It's like creating two objects that do the same purpose instead of just one. This is more efficient.


However, this can lead to problems down the line, where multiple scripts are changing a variable and other scripts are reading that value. This creates a lot of bugs and spaghetti-code issues, where you are retrieving a value at some point in time and it returns a completely different value than what you expected. This is easily solved using multiple fresh new instances of that variable, for that purpose.


In summary, if you are putting read-only variables in a static script, so you only need to create one instance of them, this is great performance-wise. However, if you put read/write variables in the same place, the code flow can get messy and confusing as to what scripts are changing and reading this variable at one time. If that makes sense.

Comment
Add comment · Show 2 · 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
avatar image unity_BjXiCscS4nd7gw · Jan 31, 2021 at 11:40 AM 0
Share

Thank you very much for your answer @Llama_w_2Ls. I was afraid of going with this implementation and find myself having unexpected bugs, but the solution you gave me sounds great, I will make variables read-only in a static script, and if I need to change them I will make another instance of them in the corresponding script.

avatar image VeryAnnoyingCat unity_BjXiCscS4nd7gw · Jan 31, 2021 at 12:08 PM 1
Share

You seem to be new here.
Please accept answers that solved your question. There should be a button under the answer.

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

110 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 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 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 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 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

Keeping track of multiple instances of one prefab 3 Answers

Find all instances of a script in the scene? 1 Answer

NullReferenceException with a text array 1 Answer

How to change a large number of booleans as fast as possible without sacrificing framerate? 1 Answer

Unity5 sprite.create perfomance issue in editor? 0 Answers


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