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類中的CheckItem()不是靜態的。您需要一個庫存實例來調用它。 – John3136 2014-09-11 06:29:25