Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
11 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 /
avatar image
0
Question by GentleRaptor · Jul 21, 2015 at 08:05 AM · gameobjecttagsnon-static

Non-static member

This script is suppoused to activate a button, from a value of 25, if the value is to low, it's suppoused to activate something else.

 using UnityEngine;
 using System.Collections;
 
 public class RepairBuyButton : MonoBehaviour {
 
     public GameObject cam;
     public GameObject BuyButton;
     public GameObject NotReadyToBuy;
 
     public void BuyItemButton()
     {
         if (playerMoney.money >= 25) 
         {
             BuyButton.SetActive (true);
         }
 
         else 
         {
             NotReadyToBuy.SetActive (true);
         }
     }
 }
 

The line with: if (playerMoney.money >= 25) is telling me :An object reference is required to access non-static member `playerMoney.money'. Help is appreciated.

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
2

Answer by allenallenallen · Jul 21, 2015 at 08:39 AM

Set up a public script and drag that script in the editor.

 public NameOfScript playerMoney.


That or, make the "money" variable a static one.

Comment
Add comment · Show 5 · 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 GentleRaptor · Jul 21, 2015 at 08:43 AM 0
Share

how do i make it a static one?

avatar image allenallenallen · Jul 21, 2015 at 08:44 AM 0
Share
 public static int money;
avatar image GentleRaptor · Jul 21, 2015 at 08:57 AM 0
Share

Could you explain me a bit mor about how to make the "if" money-value static?

avatar image allenallenallen · Jul 21, 2015 at 09:04 AM 0
Share

You don't know how accessing other scripts work, do you?

Your variable "player$$anonymous$$oney.money" is from another script, right?

In order to access that variable in this RepairBuyButton script, you can either make that variable static or access an instance of that player$$anonymous$$oney script.

Assu$$anonymous$$g "player$$anonymous$$oney" is the title of that script, go to that script. You should have typed somewhere

 public int money = 0; // Or something like that.

Just add "static" to it.

 public static int money = 0;

Now that you made this variable static, you can access it from all the other scripts and the value of that variable will stay the same for all scripts. It's like a global variable now.

Note that we're not making the "if" statement static. We're making the variable static. This has nothing to do with that if statement.

avatar image GentleRaptor · Jul 21, 2015 at 09:04 AM 0
Share

ok, i think it's working now

avatar image
0

Answer by ReCoF · Jul 21, 2015 at 09:16 AM

The problem is that you are trying to use "playerMoney" which is not declare anywhere.

So it looks like that playerMoney is a class, so you can just define your money attribute like that :

public static float money;

in your playerMoney class.

I suppose that you have only one player, so this is a good solution.

BUT, if maybe one day you will need to have two players, then i highly recommend you to add an attribute in this class :

public PlayerMoney playerMoney;

and then, from the editor, you drag your script to the new field "playerMoney".

You can do this only if your PlayerMoney inherit from MonoBehavior.

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

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

24 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

Related Questions

Checking for closest gameobject with tag. 5 Answers

Change gameobject tag when player collides with another gameobject 1 Answer

what is the purpose of 'tags' in an empty game object? 1 Answer

Making an array that contains object with one of 2 tags 2 Answers

Why do we use the "transform." in hit.transform.gameObject.tag? 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