Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 belamessex · Mar 22, 2017 at 07:45 PM · performancescriptingbasicsstatic variablestatic class

Performance when updating a static class every frame via another script?

Hey There, I'm rewriting some of my code so that a bunch of instance variables are now stored as static variables in a static class called StaticVariables. For example: one of these static variables is a bool PlayerGrounded, which is updated every frame in my main character's monobehaviour script. Before switching variables to this static class design, many scripts in my game needed check her grounded state, ie: GameObject.Find("Player") ---> GetComponent<PlayerScript>().PlayerGrounded (or some other assignment method). Now they can all just check StaticVariables.PlayerGrounded, which is what I was going for and so far works great.

My worry is that this approach of updating a static variable of a static class inside another script every frame is less efficient than using a local variable like I did before. So far I haven't noticed any issues, but I just want to make sure before I get too deep in this direction. Any thoughts?

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 jmgek · Mar 22, 2017 at 08:43 PM

As best practice you should be updating your playerGrounded in the Player class, it makes no sense to update your player by using the ground or an external class, there are multiple reasons as to why you want the logic of the class to drive itself (How are you going to tell AI that it's grounded...)

As for your concern there is no difference if you update a static or a instanced variable every frame, In theory, a static should perform slightly better than an instance, all other things being equal, because of the extra hidden this parameter.you're still going to have the exact same hit to preformance as you would if it was a instanced (NOT USING GAMEOBJECT.FIND) of the variable. Using Gameobject.find will kill your performance on every tick!

A static variable comes into existence before execution of the static constructor, this is why you cannot instance new 'Instances of it' at runtime, since you're using a single bool in this case you should use a static, I would even go so far as to suggest a singleton in your case if you continue to use external classes to drive other classes.

Comment
Add comment · Show 1 · 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 belamessex · Mar 22, 2017 at 08:53 PM 0
Share

Cool, yes. This is exactly the answer I was looking for. PlayerGrounded is updated in the player class, but it is updated as StatitcVariables.PlayerGrounded so that the StaticVariables class always has the most up to date information. All I was wondering is if that messes up performance, but I do understand the implications of using static classes vs singletons.

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

84 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

Related Questions

More than one Update/scripts Vs Single Update. Any performance advantage? 1 Answer

Should you use = null to free memory for variables declared in Start()? 3 Answers

To count deaths with global variable 1 Answer

Static member cannot be accessed 0 Answers

3 Questions (performance,static variabales,transfrom) 2 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