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 Lumind · May 10, 2014 at 07:36 AM · script.script errorpublic static

Static member cannot be accessed

Hi, I have this in script#1:

 public static  int Usetype = 0;

and this line I have in script #2:

 public int type = 0;

 Namebuild.GetComponent<UseMainBuildType>().Usetype = type;

The last on gives an error: Static member `UseMainBuildType.Usetype' cannot be accessed with an instance reference, qualify it with a type name instead I saw similar questions and solutions but they are some different and I can't use them

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 robertbu · May 10, 2014 at 07:37 AM 0
Share

So what is Use$$anonymous$$ainBuildType? Is this the class or an instance?

avatar image Lumind · May 10, 2014 at 07:45 AM 0
Share

it's a script(public class Use$$anonymous$$ainBuildType)

2 Replies

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

Answer by Bunny83 · May 10, 2014 at 09:16 AM

Static members belong to the class itself and not to an instance of that class. GetComponent retrieves a reference to an instance. To access a static member you only need to use the class name:

     UseMainBuildType.Usetype = type;

Keep in mind that since static members don't belong to a specific instance, the variable only exists once and is shared between all instances of that class.

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 Lumind · May 10, 2014 at 09:47 AM 0
Share

thanks, new info for me)

avatar image
0

Answer by VIPINSIRWANI · May 10, 2014 at 08:40 AM

I am using same but its working..... See Class First

 public class test1 : MonoBehaviour {
     public static int a = 1;
 }

and Class Second

 public class test2 : MonoBehaviour {
     
     void Start () {
       if(test1.a == 1)
             Debug.Log("Working...");
     }
 }
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 Lumind · May 10, 2014 at 09:00 AM 0
Share

yes, that works, but Oh man. I'm sorry, I wrote not those lines with which I have a problem! (those were from another script). In Script #2 the line is:

 public int type = 0;

 Namebuild.GetComponent<Use$$anonymous$$ainBuildType>().Usetype = type;

and here I have this error: Static member `Use$$anonymous$$ainBuildType.Usetype' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d I'm sorry but I still haven't solved it :)

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

23 People are following this question.

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

Related Questions

First Person Controller Scripts Are Not Working! 1 Answer

How to use method of other script of different unityPackage that i import? 1 Answer

My FOV for enemy script is not working 2 Answers

what is a script component?? 0 Answers

Some public methods can not be called 2 Answers


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