错误CS0120:非静态字段,方法或属性' newdoryui.newdiamondtext'

发布于 01-23 12:38 字数 954 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怕倦2025-01-30 12:38:06

这是指字段(或方法或任何内容)newDiamondText不是静态的,但是您正在尝试使用class inventoryui而不是通过静态引用来调用它对该类的实例的引用。

如果您的意思是newDiamondText是静态的 - 即,只有一个,每个副本都不是一个 - 您可以通过标记newdiamondtext静态。

否则,您需要通过参考inventoryUi的实例访问它 - 例如(但可能不完全)):

InventoryUI inventoryInstance = new InventoryUI();
inventoryInstance.newDiamondText.text = playerInventory.NumberOfDiamonds.ToString();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文