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){
    while (w<0){
    else if(Input.GetButtonUp("Fire2")){

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.

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:


Hope this helps clarify things!

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;