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
2
Question by WeeGee9000 · Nov 06, 2013 at 11:09 PM · variablesstatic

Can one use of a static variable, change it globaly?

Hey. Well, lets say i write a static variable called... um..... jiggy_wiggy, and i use this static variable in 2 scripts.

If i change jiggy_wiggy's value in script 1, will the value change in script 2, or will it just change in script 1?

I know its confusing.... sry.

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 Aspirer · Nov 09, 2013 at 12:58 AM 0
Share

Yes, sir. A static variable is essentially a "class specific" global variable, in that only a SINGLE variable is created, and is accessible to each instance.

In fact, a "public static" variable is 100% identical to a global variable. In case this makes you wonder why a person would use one ins$$anonymous$$d of a global, it's for the sake of OOP. I use a public static variable in my EnemySpawner class:

 public static int EnemyCount;

That way, my spawners can keep a running tally of the enemies in the area, but it's still accessible to my enemies (allowing the enemy object to decrement the value upon death).

2 Replies

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

Answer by Crazydadz · Nov 06, 2013 at 11:12 PM

jiggy_wiggy is reference to the same point in the 2 script...So yes the value will change for the 2 script. The best way to learn programming is to do some test. Create 2 different script with a static variable, change that variable in the other script and Debug.Log the value of the variable in the other script.

[EDIT] Answer not very accurate, see comments below [/EDIT]

I wasn't enough accurate in my answer thanks to @aldonaletto:

@aldonaletto comment: "NOTE: If you have a script "Script1.js" with a static variable jiggy_wiggy declared and another script "Script2.js" with another static variable jiggy_wiggy, they aren't the same! Inside Script1 you can read its jiggy_wiggy variable directly, but Script2's variable must be accessed as Script2.jiggy_wiggy. Conversely, inside Script2 you can read its jiggy_wiggy directly, and must access Script1 variable as Script1.jiggy_wiggy. In any other script you must specify the "class name" (Script1 or Script2) in order to access the desired jiggy_wiggy."

Comment
Add comment · Show 6 · 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 MarkD · Nov 06, 2013 at 11:14 PM 1
Share

Crazydadz is right, that is the reason why it is called static, because it is global :). Don't forget to mark his answer, to many people forget to do it and then the answer never gets closed and he doesn't get credit for the correct answer :)

avatar image vexe · Nov 06, 2013 at 11:41 PM 1
Share

For more details, please see my answer here (the good coding habits part) - It's very important to get you head straight about statics - as they're well known as new-comers worst nightmare :) - Think twice before using a static variable "Do I really need it here?" 99.99 percent you won't, and yet could get away, with elegant solutions :)

avatar image WeeGee9000 · Nov 06, 2013 at 11:47 PM 0
Share

how was this classified as an awnser by itself? o_O

well anyway, thanks for clarifying.

avatar image aldonaletto · Nov 06, 2013 at 11:52 PM 3
Share

NOTE: If you have a script "Script1.js" with a static variable jiggy_wiggy declared and another script "Script2.js" with another static variable jiggy_wiggy, they aren't the same! Inside Script1 you can read its jiggy_wiggy variable directly, but Script2's variable must be accessed as Script2.jiggy_wiggy. Conversely, inside Script2 you can read its jiggy_wiggy directly, and must access Script1 variable as Script1.jiggy_wiggy. In any other script you must specify the "class name" (Script1 or Script2) in order to access the desired jiggy_wiggy.

avatar image vexe · Nov 07, 2013 at 12:08 AM 1
Share

Yet another reason why I dislike JS - It hides the concept of "class", behind "Script" - A Script is actually a class (technically speaking, a script is any file with any piece of code in it) yet in JS you don't see any indicators of that. i.e. when you first create a JS file, you're implicitly creating a class! But you don't see no class something : $$anonymous$$onoBehaviour - but it's actually there, invisible!

If you have a script "Script1.js" with a static variable jiggy_wiggy declared and another script "Script2.js" with another static variable jiggy_wiggy, they aren't the same!

@WeeGee9000 if that confused you, let me explain it, in C#:

 public abstract class Enemy : $$anonymous$$onoBehaviour
 {
    // stuff...
    public static int counter = 0;
    public Enemy() { counter++; }
 }
 public class Zombie : Enemy { }
 public class Licker : Enemy { }
 
 ...
 
 Enemy Enemy1 = new Zombie();
 print (Enemy1.counter); // prints 1
 Enemy Enemy2 = new Licker();
 print (Enemy2.counter); // prints 2

The variable counter is shared (global) between ALL Enemy instances (regardless of what type it is) - So you might have a Zombie attached to some gameObject, a Licker attached to another, all those will have the same counter.

Now, if you come and do:

 public class Bullet : $$anonymous$$onoBehaviour
 {
    public static int counter = 0;
    public Person () { counter++; }
 }

That counter has nothing to do with the Enemy counter, because they're different classes, they don't relate to each other.

So, different static variables in different "scripts" (classes) are totally separate things.

Show more comments
avatar image
1

Answer by Aspirer · Nov 09, 2013 at 01:04 AM

Yes, sir. A static variable is essentially a "class specific" global variable, in that only a SINGLE variable is created, and is accessible to each instance.

In fact, a "public static" variable is 100% identical to a global variable. In case this makes you wonder why a person would use one instead of a global, it's for the sake of OOP. I use a public static variable in my EnemySpawner class:

 public static int EnemyCount;


That way, my spawners can keep a running tally of the enemies in the area, but it's still accessible to my enemies (allowing the enemy object to decrement the value upon death).

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 LukeNukem44 · Apr 19, 2018 at 08:12 AM 0
Share

Thank you. This was helpful and it actually answered the question asked.

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

21 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

Related Questions

How can I edit my static variable in the editor? 1 Answer

passing variables through scripts 0 Answers

Static variables optimization 2 Answers

My static variable changes, but other scripts don't notice. 3 Answers

The name 'Joystick' does not denote a valid type ('not found') 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