Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Rey · Dec 21, 2010 at 03:18 AM · errorstaticinstancenon-staticbce0020

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fireDude67 · Dec 21, 2010 at 05:23 AM 0
Share

Ins$$anonymous$$d of w=w+64 and w=w-64, try using the += and -= operators

avatar image Rey · Dec 21, 2010 at 07:33 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Berenger · Dec 21, 2010 at 10:11 AM 0
Share

You answered quicker and better than me, damn it ! :p

avatar image Pangaea · May 28, 2013 at 06:35 PM 0
Share

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.

avatar image
0

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; }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges