Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by RandomCharacters · Jun 17, 2016 at 06:22 AM · object referencenon-static

(yet another) error CS0120: An object reference is required to access non-static member

OK, don't be one of those dutchbags and say 'I went to google and found...' I did and found an endless loop of errors but none that helped me. I know what the issue is, but can't code it right. this should be pretty simple for you to answer.

On script #1 (UpdateStats) I have a variable "public string sceneStudyMode;". In the inspector I type in a name.

On script #2 (PlayerPrefsSTudy), I want to access it. I tried "UpdateStats.sceneStudyMode" and I get the error.

         ////////////////////////////
             /// Study Mode
             if (PlayerPrefs.HasKey (UpdateStats.ppText + UpdateStats.sceneStudyMode + UpdateStats.victoriousText)) 
             {
                 studyModeVictory = PlayerPrefs.GetFloat (UpdateStats.ppText + UpdateStats.sceneStudyMode + UpdateStats.victoriousText);        //Load                    
             }
             else
             {            
                 studyModeVictory = 0;
             }
         //////////////////////////

I think that it has something to do with referencing. I know what needs t be done. The variable is not in a method.

Thanks for the help and not disrespecting me.

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 Bunny83 · Jun 17, 2016 at 12:20 PM 0
Share

I moved the question to the Help Room as this is not a Unity specific question. This is a very basic program$$anonymous$$g question and as such doesn't belong to the default space. Please read the FAQs and the User guide.

Also you should read some basic OOP books / tutorials as you seem to to not understand the fundamental concept of objects, instances and classes.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by toromano · Jun 17, 2016 at 06:53 AM

You need to get an instance of UpdateStats and access that instance's sceneStudyMode member. Or you should make sceneStudyMode static to access from anywhere.

If you need only one instance and access from anywhere freely you might wanna check Singleton pattern out : http://wiki.unity3d.com/index.php/Singleton

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 RandomCharacters · Jun 17, 2016 at 04:38 PM 0
Share

"You need to get an instance of UpdateStats and access that instance's sceneStudy$$anonymous$$ode member"

great, but how does one code that?

avatar image
0

Answer by tanoshimi · Jun 17, 2016 at 07:58 AM

Like the message says, you need an object refetence So add one at the top of script 2:

 public UpdateStats updateStats;

Assign the UpdateStats component to that variable by dragging it into the slot in the inspector. And then use that object by replacing all your exsiting references to "UpdateStats.victoriousText" (the class) etc. to "updateStats.victoriousText" (the instance of the class as dragged into the inspector)

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 RandomCharacters · Jun 17, 2016 at 04:34 PM 0
Share

The updatestats game object and script are in another scene. So how would I drag it into the slot in the inspector?

I see what you mean here in the code with "public UpdateStats updateStats;". So I use the class name and another name. Then here just use the other name as a reference. However, the script is on a game object in another scene.

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

44 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 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

non-static member error 1 Answer

Object reference not set to instance of an object? 1 Answer

using Createassetmenu attribute and get object reference error 0 Answers

AndroidJavaClass Call non-static method from Jar to return String with CallLog 1 Answer

“Object reference not set to an instance of an object 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