- Home /
Range of Unitys Variables
I need some help with unity-javascript-variables. It's about the basics. The documentation unfortunately is not so good here. This is
what I understood so far:
Default variables are member-vars (appear in inspector, get saved with project, limited to the script-instance)
Static-vars are a special-case for people who write classes (forces vars to keep original in instances).
Public vars are global and can be accessed from any script
My questions:
A) What is private doing exactly? I can't declare private vars in functions. When only public vars are accessible from other scripts, what do I need private-vars for (beside of hiding them in inspector)?
B) Are vars in functions automatically limited to a function?
C) To access other script's variables, do I really have to declare them there with public (so only outside functions?)?
D) How do public (=global- vars) behave on script-instances (when they get declared several times)?
E) Why should I declare variables in Awake() and Start(), when they are limited to these functions then?
Thanks for answers in andvance.
Hmm, I'm not sure, but as far as I have understood the rules, questions like these belong to the unity community forum, not answers. Could a moderator or anyone else correct me if I'm wrong?
From unity-faq:
What kind of questions can I ask here?
Unity questions, of course! As long as your question is: detailed and specific written clearly and simply of interest to at least one other Unity user somewhere
and:
Don't post multiple unrelated questions in one post. $$anonymous$$ake separate posts ins$$anonymous$$d, each with one question. Don't write things that will be irrelevant in a few days.(...)
Break multi-part problems into simpler questions. Ins$$anonymous$$d of posting a question: "How can I make a gui toggle to change the skybox?", post two simpler questions ins$$anonymous$$d: "How can I make a gui toggle?" and "How can I change the skybox?" This way the questions will also be relevant to people who want to make a toggle for something else, or who want to change the skybox based on something else.
Does all fit to my question. Looks to me as if I posted at the right place.
It's not a discussion question, so it seems relevant to UnityAnswers to me.
@spacepilot, patience. We're answering questions but don't expect to get an answer right after you ask it.
Answer by Statement · Dec 22, 2011 at 01:06 PM
Static variables are shared across all instances of a class. Think of it as a global variable, one that isn't tied to a specific object. To access a static variable, you don't need an object. There exist only one copy of the static variable.
...
A) Private members (variables/functions etc) are members that can't be accessed outside your class. Its a common practice to hide as many variables as possible so other scripts can't access them. This is to hide the inner working details of a class to other classes so you don't start depending on the internal implementation details. Don't show what you don't have to. It can be used for internal timers or other stuff that the rest of the code shouldn't know about. You can't modify access levels for variables in a function. It would make no sense to do so since you couldn't reach them anyway.
B) Yes. They only exist inside the function.
C) Yes. Outside functions.
D) Public variables aren't really "global". I don't know if you mix it up with static variables, which are globals. A public variable is just a variable that you can access. A public variable can be a static variable, and a static variable can be private as well. public/protected/private/internal etc are access modifiers that tell if a member can accessed or not. static variables are variables that doesn't have any instance copies, there is only 1 instance of that static variable and it's shared across all objects of that class.
E) You shouldn't. However, JS has this feature that you can run code in the "class body", mixing the variable definitions and the startup code. That is when you aren't using
function Start() { ... }
. You can however initialize variables in Start or Awake if it applies to the problem you want to solve (like querying for other objects or precomputing some tables).
A+C) So I can access other scripts variables even when they are not public(=member-vars)?
D) Did I get it right then that only function- and private vars are inaccesible from other scripts?
When public ist not really global then I still don't get the difference between member and public vars.
E) Are member-vars, delcared in Awake and Start, available in all other functions of a script? Then they should be private automaticall. Is that true?
You need them to be public to access them. That's what public means. There are other modifiers that can be used in different scenarios such as protected, where subclasses can access members from base classes and internal which allow the entire assembly to access it, but not other assemblies. But if you're starting out, just use public and private, and if you want a private variable accessible in a subclass, then you can change it to protected.
Yeah, private members are only accessible from the script that define them.
A public member variable is a member variable. The public only means that other classes also can access it.
You can't declare member variables in Awake or Start since they are functions. If you create any (local) variables in a function, they will only exist for the duration of that function call.
Basically: If you have code in another class(script) that need to access a member, it's probably best to make it public. If you don't have code in other classes that need the member, it's probably best to make it private. Any variables that you need to access from another function or another class should be a member variable. Again, make sure to decide if it should be public or private. If the variable should be unique per object, you shouldn't make the variable static. If the variable should be the same across each object, it should be static. If you change the value of the static variable, it's like you changed the variable on all objects. Don't declare variables inside functions if you want the variable to persist after the call, they will expire when the call is completed.
What I got now:
-Function-vars get lost at end of functions (unlike script-vars!)
-Classes can access usual member-vars of other classes.
-Scripts can only access the public-vars of other scripts
==> Private is for protecting vars from other classes
==> Usual member-vars are protected from other scripts (but not from other classes)
==>Public is only for access from other scripts then.
==>Usual member-vars are inaccessible by other scripts.
Is this correct? Do private-vars get lost at the end of a script then?
In any case: It seems to me then that there is no reason to use private then, unless I declare several classes in my script.
Answer by Eric5h5 · Dec 22, 2011 at 01:09 PM
A) You need private for variables that are accessible to all functions in a class, but should not be accessible to anything outside that class. Somewhat similar is "protected", which is like private, except that classes derived from that class can also access the variable.
B) Variables declared in functions are limited in scope to that function.
C) They have to be public, or in some cases protected as described above.
D) Not quite sure what you mean. All variables are instanced per-script, unless they are static, if that's what you're getting at.
E) You should declare variables in Awake or Start if they are only needed in those functions and aren't used by any other functions.
C) But then I don't need private
D) Yes, this is what I was looking for. What is the sense of public then?
E) The doumentation makes no sense to me. Awake is only run once and should be used for initialization. Initialization makes no sense when the vars get lost afterwards.
C) Yes you do.
D) I don't understand the question. As explained several times, public variables can be accessed by other classes, private variables cannot.
E) As I said, if you're using variables in Awake and don't need them to be accessed outside of Awake, then declare them in Awake. For example, if you have complex initialization code that makes use of some variables, but those variables don't need to be used by other functions, only Awake. There's nothing special about Awake or Start, they are just functions like every other function, and all functions and variables everywhere use the same rules.
C) LOL :-D
D) What's up with usual members then? Who has access? Who not?
E) Thanks, I think I got it now. Initialization is usually loading-time of a game at the beginning, done only once. It's useless when the variables get lost. So to use Awake, I should declare all variables before to only fill them in such an Awake-function. That's important to know and of course not mentioned in the documentation.
D) If by "the usual members" you mean variables with no access modifier, that depends on the language. In Unityscript, the default is public. In C#, the default is private. e.g., "var foo : int;" is the same as "public var foo : int;".
E) Well, no. Awake runs once when a script is initialized, which may or may not not be at the beginning. For example, instantiating a prefab that has a script with an Awake function attached. It's not "useless" when variables get lost--you may only need certain variables in Awake, in which case it's useless (and bad practice) to make them global. This isn't something that really needs to be mentioned in the docs, because it's a basic code concept, not something specific to Unity.
...it's propably a basic concept in object-oriented program$$anonymous$$g. I've never heard of it and only use simple stuff usually - but since a long time(e.g. Javascript, Quickbasic, Pascal, Batch-stuff, Sh-scripts, Autoit,...). Never heard of an Awake-function though. Since Unity's Javascript is a special-version of javascript, this is unity-specific and really could get mentioned in the docs. I bet it got implemented because of C# and I think basics of a program$$anonymous$$g-language always should be in the docs. $$anonymous$$issing also in the docs: List of loops, list of conditions (if, select-case,exiting functions...), overview of var-types and their "access-modifiers",... The unity-documentation actually is extensive. However, things like this always need to get asked here. Another example: Arrays.
The question actually is answered anyway. You can tell me how to exit a function on a condition, when you feel boring. However, I see no reason to discuss the sense of a var-loosing init-function now. You can open a new threat for this or answer this question from me I got no answers on, up to now: Drawing icon in editor