public class BuyableItem : MonoBehaviour
{
public float PickUpRadius = 1f;
public InventoryItemData ItemData;
private SphereCollider myCollider;
private void Awake()
{
myCollider = GetComponent<SphereCollider>();
myCollider.isTrigger = true;
myCollider.radius = PickUpRadius;
}
private void OnTriggerEnter(Collider other)
{
var inventory = other.transform.GetComponent<InventoryHolder>();
if (!inventory) return;
if (inventory.InventorySystem.AddToInventory(ItemData, 1))
{
Destroy(this.gameObject);
}
}
public static void UpdateDiamondText(PlayerInventory playerInventory)
{
InventoryUI.newDiamondText.text = playerInventory.NumberOfDiamonds.ToString();
}
}
How would I go about fixing this issue?
Error CS0120: An object reference is required for the non-static field, method, or property 'InventoryUI.newDiamondText'
CodePudding user response:
It means the field (or method or whatever it is) newDiamondText
isn’t static but you’re trying to call it using the static reference to the class InventoryUI
rather than via a reference to an instance of that class.
If you meant newDiamondText
to be static — i.e. there’s only one of it ever, not one for each copy of the class — you can solve the problem by marking newDiamondText
as static
.
Otherwise, you’ll need to access it via a reference to an instance of InventoryUI
— e.g. something like (but probably not exactly) this:
InventoryUI inventoryInstance = new InventoryUI();
inventoryInstance.newDiamondText.text = playerInventory.NumberOfDiamonds.ToString();