- Home /
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.
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.
Could you explain me a bit mor about how to make the "if" money-value static?
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.
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220611232256im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Deleting gameobject with tag problem 1 Answer
Hi, getting error cs1041 in my code:identifier expected, "this" is a keyword; need solution please 3 Answers
Is there a way to get the actual joystick.Vertical and joystick.Horizontal Value?, 0 Answers
Disabling GameObjects With A Tag That Are More Than A Set Distance From A GameObject? 2 Answers