- Home /
An instance of type "x" is required to access non-static variable "y"
I have seen a lot of questions relating to this error, but I haven't been able to find anything that fixes my problem. I think the problem is that a static variable cannot access a non-static variable. I need to have a non-static variable access a non-static variable from another script attached to the same object.
The first function, "shift" defines the variable w and manipulates it.
var w : float ;
function Update () {
while (w>=1536){
w=w-1536;
}
while (w<0){
w=w+1536;
}
if(Input.GetButtonUp("Fire1")){
Debug.Log("down");
w=w-64;
}
else if(Input.GetButtonUp("Fire2")){
Debug.Log("up");
w=w+64;
}
}
in the other script, I tried writing:
function Update () {
var t : float = shift.w;
//the script does a bunch of irrelevant stuff with t
}
If I do the same thing, but instead reference the objects transform value (which is also non-static), it works fine, which makes me think that it should be possible to get this to work.
Ins$$anonymous$$d of w=w+64 and w=w-64, try using the += and -= operators
That is a bit of an easier way to do it, thanks for the tip (I am new to javascript, so I am not entirely familiar with it), but it doesn't help the main problem.
Answer by duck · Dec 21, 2010 at 10:01 AM
It sounds like your problem is that you don't understand how references work in unity yet. Read more about it here to get started (and upvote any answers which help!).
Assuming you've read that, you should now understand that you need a reference to the instance of that script which is on the gameobject, rather than a reference to the script itself.
So your script is called "shift"? (It's convention to name scripts and functions with an initial upper case letter, so rename your script, and I'll continue with it as "Shift").
In that case, you either need to create a public variable whose type is "Shift", which will create a visible slot in the inspector, and drag your gameobject into that slot:
var shiftInstance : Shift;
Or, create a private variable whose type is "Shift" (which won't show up in the inspector), and do a "GetComponent" in your object's Start() or Awake() function which grabs the reference:
private var shiftInstance;
function Start() { shiftInstance = GetComponent(Shift); }
Once you've done either of these, your "shiftInstance" variable now contains a reference to the instance of the "Shift" script which you placed on the gameobject, and you should then be able to access the variables on it, for example:
shiftInstance.w
Hope this helps clarify things!
When dragging the game object into the empty variable slot, what object do I drag? The only option that appears is the PlayerController, and I only want a specific script to be reference.
Answer by Berenger · Dec 21, 2010 at 10:09 AM
What do you mean it works with objects transform ? I tried the scripts in an empty project, two empty game objects and those two scripts. There is indeed the non-static error with w, but I got the same with
var t : Transform = Shift.transform;
And it makes sense. Those variables must be used with an instance of the Shift script, meaning :
var shift_instance : shift;
function Update () { var t : float = shift_instance .w; // or var t : Transform = shift_instance .transform; }
Your answer
Follow this Question
Related Questions
An instance of type 'TurretControl' is required to access non static member 'Shoot'. 1 Answer
An instance of type X is required to access non static member Y [javascript] 4 Answers
Problem setting up an array with a size determined by a variable 2 Answers
Help with Error: An instance of type 'Regex' is required to access non static member 'Replace'. 2 Answers
simple coding problem 1 Answer