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:

  1. Default variables are member-vars (appear in inspector, get saved with project, limited to the script-instance)
  2. Static-vars are a special-case for people who write classes (forces vars to keep original in instances).
  3. 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.

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) 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.