错误CS0120:需要对象引用才能访问非静态成员
Unity相当新颖,我已经通过我的游戏获得了不太多的打嗝,但我难以忍受这一次。 我得到的错误是:错误CS0120:需要对象引用才能访问非静态成员
资产/脚本/ Interactables/ButtonMoveObject.cs(25,55):错误CS0120:一个对象引用需要访问非静态成员`Inventory.CheckItem(库存.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;
}
}
}
您也需要类的一个实例库存:
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) {... }
好吧,我明白了,我已经修好了,但现在我现在正在收到其他错误。我编辑了我的主帖。 – An0nx 2014-09-11 06:49:58
Inventory类中的CheckItem()不是静态的。您需要一个库存实例来调用它。 – John3136 2014-09-11 06:29:25