- Home /
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 ...
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
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.
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()
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.
what if you want to organize all the specific gui element functions into one file, would this case of static be applicable?
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
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;
}
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
Follow this Question
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