错误CS0120:访问非静态成员需要对象引用

时间:2014-09-11 06:26:36

标签: c# static unity3d

对Unity来说相当新,而且我没有太多打嗝而已经通过我的游戏,但是我很难接受这一点。 我得到的错误是:

  

Assets / Scripts / Interactables / ButtonMoveObject.cs(25,55):错误CS0120:访问非静态成员`Inventory.CheckItem(Inventory.items)'

需要对象引用

我似乎无法弄清楚,有什么想法吗?

using UnityEngine;
using System.Collections;

public class ButtonMoveObject : MonoBehaviour {

//Object name to link to button
public GameObject buttonReceiver = null;

//Target for moved objects and time it takes
private Vector3 source;
public Vector3 target;
public float overTime;

//Prerequesites required to operate it
public Inventory.items[] pres;

//Flag to keep checking prerequisites and check count
private bool checkFlag = true;
private int checkCount = 0;

void Use ()
{
    if (pres.Length > 0) {
        while (checkFlag) {
            checkFlag = Inventory.CheckItem(pres[checkCount]);
            if (checkCount == pres.Length) {
                if (checkFlag) {
                    checkFlag = false;
                    StartCoroutine (MoveObject ());
                }
            }
            checkCount++;
        }
    }
}

IEnumerator MoveObject()
{
    source = buttonReceiver.transform.position;
    float startTime = Time.time;
    while(Time.time < startTime + overTime)
    {
        buttonReceiver.transform.position = Vector3.Lerp(source, target, (Time.time - startTime)/overTime);
        yield return null;
    }
    buttonReceiver.transform.position = target;
}

}

编辑:

好的,我已用checkFlag = player.GetComponent<Inventory>().CheckItem(pres[checkCount]);修复了它 但现在我收到错误Assets/Scripts/Player/Inventory.cs(21,30): error CS0176: Static member Inventory.items.keycardGreen' cannot be accessed with an instance reference, qualify it with a type name instead 在我的库存脚本中

using UnityEngine;
using System.Collections;

public class Inventory : MonoBehaviour {

public enum items
{
    keycardGreen,
    keycardRed
};

//Inventory List
[HideInInspector]
public bool keycardGreen = false;
[HideInInspector]
public bool keycardRed = false;

public void CollectItem (items newItem)
{
    switch (newItem) {
    case newItem.keycardGreen:
        keycardGreen = true;
        Debug.Log(newItem + " collected.");
        break;
    case newItem.keycardRed:
        keycardRed = true;
        Debug.Log(newItem + " collected.");
        break;
    default:
        break;
    }
}

public bool CheckItem (items checkItem)
{
    switch (checkItem) {
    case checkItem.keycardGreen:
        return keycardGreen;
        break;
    case checkItem.keycardRed:
        return keycardRed;
        break;
    default:
        Debug.Log("Item does not exist.");
        break;
    }
}
}

1 个答案:

答案 0 :(得分:0)

您需要一个类库存实例:

Inventory inv = new Inventory();
while (checkFlag) {
    checkFlag = inv.CheckItem(pres[checkCount]);
    if (checkCount == pres.Length) {
        if (checkFlag) {
            checkFlag = false;
            StartCoroutine (MoveObject ());
        }
    }
    checkCount++;
}

或CheckItem方法是静态的:

static bool CheckItem(Inventory.items item) {... }