0

我收到此错误:

访问非静态成员“Inventory.ItemID”需要对象引用

我想改成1 ItemIDInventory

班级ItemSwordUneq

using UnityEngine;
using System.Collections;

public class ItemSwordUneq : MonoBehaviour 
{
    public bool canPickup = false;

    void Update () 
    {
        if(canPickup == true && Input.GetKeyDown(KeyCode.F))
        {
            GameObject player = GameObject.Find("Player");
            Inventory inventory = player.GetComponent<Inventory>();
            Inventory.ItemID = 1;
            Destroy(gameObject);
        }
    }

    void OnTriggerEnter(Collider other)
    {
        canPickup = true;
    }

    void OnTriggerExit(Collider other)
    {
        canPickup = false;
    }

    void OnGUI()
    {
        if(canPickup == true)
        {
            GUI.Label(new Rect(250, 250, 300, 20), "Press 'F' To Pick Up Sword");
        }
    }
}

班级Inventory

using UnityEngine;
using System.Collections;

public class Inventory : MonoBehaviour 
{
    public bool hasItem = false;
    public int ItemID;
}

我究竟做错了什么?任何答案将不胜感激。

4

1 回答 1

0

你可能想写inventory.ItemID = 1;而不是Inventory.ItemID = 1;.

也就是说,如果您想将结果的 ItemId 设置player.GetComponent<Inventory>();为 1。

您试图在 classInventory而不是 object上调用方法inventory,如果该方法不是静态的,那将没有任何意义。

于 2013-08-22T05:13:39.000 回答