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
1
Question by ina · Dec 21, 2011 at 02:21 AM · variablestaticenumorganization

Why is static typing required for access of public JS function or var in Standard Assets

Trying to organize my projects into smaller chunks, breaking things up into individual scripts and putting in Standard Assets. Apparently, to reference them, I have to put a static in front of all the functions and vars in my JS...

1) Why is static typing required? What does it do, and will it break any existing script functionality (assuming all the new broken-up scripts were once in the same big huge script)?

2) Why do variables in the same script need to be typed static if called by a static function in the same script?

3) Why don't enum's need the static declaration?

BCE0020: An instance of type 'GUIElements' is required to access non static member ...

Comment
Add comment · Show 3
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 ina · Dec 21, 2011 at 03:58 AM 0
Share

the unity documentation seem to imply that putting static in from of a var makes it a globally-accessible global - ie accessible from a script without having to call GetComponent http://unity3d.com/support/documentation/ScriptReference/index.$$anonymous$$ember_Variables_26_Global_Variables.html

avatar image Lo0NuhtiK · Dec 21, 2011 at 04:07 AM 0
Share

To access it from another script you need to use the name of the script followed by a dot and the global variable name.print(TheScriptName.someGlobal); TheScriptName.someGlobal = 10;

That's at the bottom of that page, so I suppose you wouldn't have call GetComponent, but you'd still have to call it... idk, like I said in my first post (which I deleted cuz @Peter G cleared up what I was saying and explained it better) , from what all I've read everywhere, using static variables etc isn't a good idea unless you really know what you're doing with them.
avatar image Peter G · Dec 21, 2011 at 01:36 PM 0
Share

The documentation isn't wrong per se, but it is totally misleading. Ignore their statement because it neglects to mention how static fields really work. Use GetComponent()

1 Reply

· Add your reply
  • Sort: 
avatar image
4

Answer by Peter G · Dec 21, 2011 at 02:56 AM

Adding static in front of it doesn't make it static typing, there's a huge difference for another day. Do you understand the difference between class and instance variables?

Static makes it a class variable. I can tell you now, you DO NOT want this functionality. It associates a variable with the class instead of with any given instance. It changes a number of things that have to do with the behavior of the field. It is useful for something like Mathf where you want a class to be a container for all the functions and constants related to math.

Instance variables are created whenever you create a new instance of your class. In js terms, that's whenever you add a script to a game object. The only way to access them is through an instance of your class. That's why you're getting that error. It actually says exactly what you need.

Specifically your problem, when you split up your classes, you isolated different variables. Now you need to use GetComponent() to get references to the instances.

 value = GetComponent.<SomeScript>().instanceVariable;

If you don't get it, just do a little searching for static var problems. They pop up all the time and the answer is always the same. Don't use static vars.

Comment
Add comment · Show 4 · 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 ina · Dec 21, 2011 at 03:47 AM 0
Share

what if you want to organize all the specific gui element functions into one file, would this case of static be applicable?

avatar image ina · Dec 21, 2011 at 03:52 AM 0
Share

also, what about a global assignment of a gameobject that you want to do stuff too (ie, move it, rotate it etc) - that does not seem to need to be instanced. would static be appropriate there

avatar image Peter G · Dec 21, 2011 at 04:34 AM 1
Share

For that, it sounds like you want to implement a singleton/lazy-loaded method for your static variables.

You can and should use properties, but js properties are bizarre so here's a way of getting a readonly value in js.

 private static var instance : Transform;
 public static function getGlobalTransform() : Transform {
    if(instance == null) {
           instance = GameObject.Find("SomeGO").transform;
    }
    return instance;
 }

avatar image ina · Dec 21, 2011 at 10:03 PM 0
Share

Is the only way to reference a gameobject for static functions to use GameObject.Find? (Since apparently the global vars do not show up in inspector)

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

6 People are following this question.

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

Related Questions

Increasing a score value? 2 Answers

Static array variable 1 Answer

Will static variables not work if negative? 1 Answer

Reset a static variable? 1 Answer

Difficulty Accessing Static Variable 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