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 NADRAC · Jan 02, 2014 at 02:30 PM · guiclassstaticguitextnon-static

An object reference is required to access non-static member

A lot of similar questions and answers but I can't make this work. This error keeps showing up no matter what:

"An object reference is required to access non-static member CoinsCounter.Coins"

I have a Class Named Collect, which contains a public variable called Coin, this variable increases by 1 every time the player collides with an object using this script.(I guess so far this works because I can see the print on my console).

Now, the problem is that I have a GuiText and I want to use it to count and see the number of coins that I am collecting on my HUD.

I created a Class called CoinsCounter, drag it to my GuiText and inside it I have this piece of code:

using UnityEngine; using System.Collections;

public class CoinCounter : MonoBehaviour {

 // Update is called once per frame
 void Update () {
     guiText.text = Collect.Coin;
 }

}

I just want the GuiText to change the "0" value to the number of coins I am picking up.

Any idea?

Thank you very much for your time !!!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by HappyMoo · Jan 02, 2014 at 03:13 PM

Hi,

if you have your Collect class just for the purpose of counting the players coins and don't want to instatiate objects from it, you can mark everything in it static

 static class Collect
 {
   public static int Coins
 }

Then your CoinsCounter - I suggest CoinsUpdater - would look like this

 public class CoinsUpdater : MonoBehaviour
 {
   void Update()
   {
     guiText.text = Collect.Coins;
   }
 }

To understand what static means, check out this video: http://www.youtube.com/watch?v=nraOAaYLdRQ

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
avatar image
0

Answer by NADRAC · Jan 02, 2014 at 11:45 PM

Thank you very much HappyMoo!!

I tried that but now I am getting another error.

Static class Collect' cannot derive from type HitTrigger'. Static classes must derive from object.

Thank you for the video link as well.

I am not very familiar with C# as you can see.

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 HappyMoo · Jan 03, 2014 at 12:51 AM 0
Share

I see... I don't know why you have to derive from HitTrigger and if that's a bad design, but you can make the error message go away if you remove the "static" in front of the "class Collect"

avatar image HappyMoo · Jan 03, 2014 at 09:54 PM 0
Share

did that help?

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

19 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

Related Questions

Using a static class & pass in gameObject or just add same script to multiple objects 1 Answer

Static class member for offset 1 Answer

Script to change GUI text's displayed text value? 7 Answers

JS Class "Does not denote valid type" 0 Answers

What's wrong with my function? It keeps telling me 'loseText' is not a member of 'UnityEngine.GUIText'. Please help, please and thank you! 0 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