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
0
Question by choiie · Jun 19, 2014 at 10:35 PM · c#variablesreferencestatic

Problem with accessing static variables

Hi. I'm using a GameObject called References, containing a script with the same name, which contains references to other GameObjects which are activated and deactivated during gameplay. Now, I'm having problems with referencing the static variables within the References script. The error I get is:

Assets/Scripts/CharacterSelection.cs(43,87): error CS0176: Static member `References.playerRef' cannot be accessed with an instance reference, qualify it with a type name instead

and the erratic code looks like so:

 private GameObject playerRef;
 
 void Start () {
         playerRef = GameObject.Find ("References").GetComponent<References>().playerRef;
     }

The References script looks like this (the value of the variable is assigned elsewhere):

 public static GameObject playerRef;

I've been googling for help on static variables, but all I can find is how to work with static methods. If anybody could give me a pointer on what I'm doing wrong, it would be really helpful.

Comment
Add comment · Show 1
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 choiie · Jun 19, 2014 at 11:02 PM 0
Share

Here's the script which produces the error: https://github.com/choiie/AmpersandLime/blob/master/Assets/Scripts/CharacterSelection.cs

1 Reply

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

Answer by Bunnybomb7670 · Jun 19, 2014 at 10:58 PM

Ok so, instead of finding an Instance, you can refer straight to the class itself. so you would do References.playerRef; Static variables are on their own, a single instance and the way you access them is by referring to the base class, rather than finding an instance of the class within the scene. They are help separately outside of instances.

http://www.dotnetperls.com/global-variable might help a bit more with this issue.

Comment
Add comment · Show 3 · 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 choiie · Jun 19, 2014 at 11:05 PM 0
Share

So if I'd simply write References.playerRef I would get the variable in the References script? Will it automatically deter$$anonymous$$e where to find the References script?

avatar image choiie · Jun 19, 2014 at 11:07 PM 0
Share

Ok, so that worked - I read somewhere that to access variables in other scripts they need to be made static, but your solution worked fine.

avatar image Bunnybomb7670 · Jun 19, 2014 at 11:11 PM 0
Share

To access variables in other scripts they do not need to be static, as long as you have a reference to their gameObject in the world, you can use getComponent to access that specific script then retrieve that instance variable, without it being static. Static means only one, which is not always good, something such as a zombie enemy cannot have a static Health because when one is damaged, all of them would be damaged.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

how to reference a javascript variables in a c# code. 0 Answers

Trying to access a variable from another script attached to same object. 1 Answer

How do I make a list of references to a variable? 2 Answers

non-static variable behaving like static (C#). Very confused. 2 Answers

Local alternative to static variables? 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