- Home /
static var for one object.
I'm looking for a way in java script to change the static variable from a separate script only on the script of one object, that object is a variable (transform var or gameObject var ect.)
this is what i thought would do it but no luck, only errors:
var object : GameObject;
function Update () {
object.ObjectScript.StaticVar += 1;
}
Answer by PrimeDerektive · Aug 25, 2011 at 01:24 PM
There's no such thing as a static var for only one object (unless the object that has the script with that variable is a singleton). A static var in a script will only hold one value across all objects that use that script... which is why the syntax for accessing static variables is the way it is.
You probably want to make that variable a public member variable, and then access it via GetComponent, a la:
object.GetComponent(ObjectScript).publicVar += 1;
Answer by Bunny83 · Aug 25, 2011 at 01:22 PM
Static stuff is not part of the object itself. Static variables or methods belong to the class, not to an instance of that class. There is only one class of that type. If you need data that belongs to an instance remove the static to make it an actual member of the object.
Your answer
Follow this Question
Related Questions
Instantiate problem 1 Answer
Referencing children of an instantiated object. 1 Answer
Created game objects rollover show guy text 0 Answers
Quest Script Help 2 Answers
{Easy question} How to disable script from other gameObject. 2 Answers