错误CS0120:非静态字段,方法或属性' newdoryui.newdiamondtext'
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();
}
}
我将如何解决此问题?
错误cs0120:非静态字段,方法或属性“ intebtoryui.newdiamondtext”
需要对象引用
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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

这是指字段(或方法或任何内容)
newDiamondText
不是静态的,但是您正在尝试使用classinventoryui
而不是通过静态引用来调用它对该类的实例的引用。如果您的意思是
newDiamondText
是静态的 - 即,只有一个,每个副本都不是一个 - 您可以通过标记newdiamondtext
为静态。
否则,您需要通过参考
inventoryUi
的实例访问它 - 例如(但可能不完全)):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 classInventoryUI
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 markingnewDiamondText
asstatic
.Otherwise, you’ll need to access it via a reference to an instance of
InventoryUI
— e.g. something like (but probably not exactly) this: