• 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
0
Question by Luci85 · May 14, 2012 at 03:31 PM · instance

Strange error: An instance of type 'X' is required to access non static member 'Y'.

I get this really strange error from my DrawPathFullTrackScript:

 An instance of type 'DrawPathFullTrackScript' is required to access non static member 'linepos'.

What I don't get is, linepos is declared as:

 var linepos:Vector3;

in the SAME script I get the error from.
Do I have to reference a script in the same script or what is wrong here?

Comment
Add comment · Show 3
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 Drakestar · May 14, 2012 at 03:40 PM 2
Share

Post the actual line that throws the error. Generally speaking, the compiler is complaining about the fact that you're trying to call a method on a class ins$$anonymous$$d of on an instance of that class. Somewhere, doing this:

DrawPathFullTrackScript.$$anonymous$$ethodName()

But you have to do this:

var foo : DrawPathFullTrackScript; foo.$$anonymous$$ethodName()

avatar image Luci85 · May 14, 2012 at 03:41 PM 0
Share

And I only get this error from a single function, I can access the variable from every other point of the script without troubles.
The actual line is:
p.y = linePos.y + 5;
EDIT:I think it has to do with the fact that I'm calling linepos in a static function.

avatar image whydoidoit · May 14, 2012 at 03:45 PM 1
Share

And is that method declared static?

1 Reply

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

Answer by hathol · May 14, 2012 at 03:47 PM

Without seeing the rest of your code: I would guess you try to access the variable from within a static function. But since non static members only exist within an instance of an object (unlike static ones that exist in the class itself) you can't access it. Either make your member variable static or try and get an instance of your object first (using the singleton pattern might help with that)

Comment
Add comment · Show 1 · 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 Luci85 · May 14, 2012 at 03:52 PM 0
Share

Yes, that was the problem, I used the non-static variable in a static function.
I did not see the forest for the trees :-)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I need help with the location of instatiated objects on the scene and correct interaction with this 2 Answers

Creating fields/instance variables 1 Answer

ArrayLists vs Generic Lists 1 Answer

How can i play MovieTextures of Prefab instances independently? 2 Answers

How can I set the material on an instance after creating it? 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