Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by TuRtoise · Sep 19, 2017 at 11:22 AM · c#staticscore system

When should I use static fields/methods?

Hello everyone, I've just started learning Unity and at the moment I'm simply following the tutorials available on the site. It's all quite clear to me so far, but I'd like to ask you this one question which bothers me.

Assume this: We've got a 'GameController' game object with a 'Controls' scripts attached to it as a component. The script holds a 'score' value that is to be displayed on the screen. The 'score', however, must be updated inside another script, where the 'point-addition' condition is defined.

The issue is discussed in the Space Shooter Tutorial 3/02 'Counting points and displaying the score'. The eventual solution that is suggested to the viewer is to (operating on the second script) find the tagged 'GameController' game object (using FindWithTag), then create an intance of the 'Controls' class and finally use the previously written 'AddPoints' method to update the private 'score' value.

What I initially thought about when the problem was first stated was simply making 'score' and 'AddPoints' static, making operations between the scripts much simpler. It just seemed more natural to me - I think I don't really need several GameController instances, but perhaps in the long run that indeed is a bad practice... How do you think? Are such 'static operations' something I should avoid if possible and the solution shown in the video was for a reason or is it just another valid approach?

Thanks for your feedback in advance!

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 Positive7 · Sep 19, 2017 at 12:45 PM 0
Share

Experience : Create 3 primitive in your scene. Attach the script below.

 private static int staticScale = 2;
 
     public float NonStaticScale = 1.5f;
 
     public int OverrideStaticScale;
 
     private void Update()
     {
         staticScale = OverrideStaticScale;
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.N))
             transform.localScale = new Vector3(NonStaticScale, NonStaticScale, NonStaticScale);
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S)) transform.localScale = new Vector3(staticScale, staticScale, staticScale);
     }


Sum : static field will be shared among all objects, non static fields can be controlled separately.

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by NashDevsoft · Sep 19, 2017 at 11:44 AM

Hi @jfilmie glad you are enjoying learning Unity so far. It is kind of down to personal preference in my experience. Personally I tend to stay away from static fields whenever possible, however static functions are more acceptable.

Your first problem is that a static field cannot be viewed in the inspector, although this is a minor inconvenience. The second larger problem occurs most often when you are transitioning between scenes. Say you had a static field that tracked your score object. If you switched scenes the score object in the static variable would need to be manually reassigned in order to contain the correct reference. However a static method which executes GameObject.Find("score") would work identically across all scenes.

However GameObject.Find can be expensive when used in excess. A more elegant solution would be having an object that tracks the score that is flagged for Object.DontDestroyOnLoad. This can also be utilized through Singletons, but again you should be careful how often you utilize them.

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
avatar image
0

Answer by MaxGuernseyIII · Sep 19, 2017 at 12:57 PM

Short answer:

If you have to ask the question, you should probably avoid using them.

Long Answer:

You use static fields when you want something to be global. Another way of saying that is "almost never". As @NashDevsoft mentioned, you can use static fields to support the Singleton pattern but it is important to be judicious about that.

The big risk is coupling. If you have a singleton that is write-only (like something that dumps analytics into the cloud) or one that is read-only (like something that tells you about the current device), you're probably okay. Purely functional singletons are fine, too.

However, a lot of people fall into the trap of making read/write singletons. These are nothing more than glorified global variables because anything can couple to anything else indirectly by way of the singleton. Once you have something like that, you run the risk that any change anywhere in your system can impact any other thing in your system.

Once you are in that position, you're always behind the eight ball. Testing becomes over-burdensome and your code is at constant risk of decay.

Comment
Add comment · Show 9 · 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 TuRtoise · Sep 19, 2017 at 08:20 PM 0
Share

Thanks for help! I guess I'll try to avoid using statics for now. Haven't thought of possible problems with scene management, that's truly worth noting. Thank you for pointing it out.

avatar image Hellium · Sep 19, 2017 at 09:25 PM 0
Share

You use static fields when you want something to be global.

You clearly seem to not know what static is.... Static means the member (either the variable or the function) belongs to the class, not the instance (every instance will share the variable / the function does not manipulate any instance)

avatar image MaxGuernseyIII Hellium · Sep 19, 2017 at 09:32 PM 0
Share

Before your comment is down-voted to oblivion, I'd be curious to find out how you arrived at your conclusion that I don't know what a static is.

avatar image Hellium MaxGuernseyIII · Sep 19, 2017 at 09:44 PM 0
Share

Because indicating that static variables must be used as "global variables" is the best way to make people write very bad code. This is clearly not the purpose of static variables, but people continue to use as it because of people like you.

Show more comments

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

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

Increase the number just when the button is pressed 0 Answers

Score system using prefabs in C# 0 Answers

Scoring system not working. 1 Answer

I have no Idea why my highscore Script isn't working ... 0 Answers

Add "1" to score text? Not "11111"? 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