- Home /
Static variables explanation.
Hey.
I have a question. I'm working on a rpg game and I'm using static var to access varibles from other scripts. It works good exept for one thing. The static varibles doesn't show up in the inspector window. Is this a bug or a feature in unity? And what other ways are there to access varibles from other scripts? I don't understand this:
http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
Thanks. //Proximal-Pyro
Answer by Chronos-L · Mar 08, 2013 at 09:20 AM
Which part of it you do not understand? Please don't say the whole thing.
public static
variables are not shown in the inspector because static
variables belong to the class itself, not the object/instance of the class.
Non-`static` variables in the other hand, belongs to the object/instance of the class. When a gameObject is attached with a script, it becomes the object/instance of the class of the script, and hence, the public
non-`static` variables will then be exposed in the inspector.
I don't understand how to access varibles. That page only explains how to access functions, right?
//Proximal-Pyro
If your variable is declared as public
, you can access them like any public
functions. The page is mainly about accessing other gameObject in the scene and how to manipulate these gameObject's component. It is not just about how to access functions.
The 3rd script in that page
// Set foo DoSomething on the target variable assigned in the inspector.
var target : OtherScript;
function Update () {
// Public variable
target.foo = 2;
// Public function
target.DoSomething("Hello");
}
Answer by whydoidoit · Mar 08, 2013 at 09:19 AM
Variables which are static are not related to the specific object and so do not show up in the inspector. Using Static may not be the best method (see GetComponent articles on Unity Gems) but if you do decide on that route - you can create a script that has normal variables which it puts into the statics on Awake - then you have a way of defining them in the inspector.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Static variables 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Native visual scripting? 0 Answers
Pain in the ass 2 Answers