【问题标题】:error CS0120: An object reference is required to access non-static member错误 CS0120:访问非静态成员需要对象引用
【发布时间】:2014-11-04 23:49:28
【问题描述】:

对 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&lt;Inventory&gt;().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;
    }
}
}

【问题讨论】:

  • Inventory 类中的 CheckItem() 不是静态的。您需要一个 Inventory 实例来调用它。

标签: c# static unity3d


【解决方案1】:

您需要一个 Inventory 类的实例:

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) {... }

【讨论】:

  • 好的,我明白了,我已经修复了它,但现在我遇到了其他错误。我已经编辑了我的主要帖子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-22
  • 1970-01-01
  • 1970-01-01
  • 2016-06-19
相关资源
最近更新 更多