我正在为Unity游戏制作一个库存类。但是,我遇到了一个问题。每当我在使用构造函数后调用方法并在其上使用+
运算符时,由于某种原因,它无法在对象内部找到任何方法。
这是课程:
public class Inventory {
public bool init = false;
public string _1 = "";
public string _2 = "";
public string _3 = "";
public string _4 = "";
public string[] In {
get {
return new string[4] { _1, _2, _3, _4 };
}
set {
_1 = value[0];
_2 = value[1];
_2 = value[2];
_3 = value[3];
}
}
public Inventory(string A, string B, string C, string D) {
_1 = A;
_2 = B;
_3 = C;
_4 = D;
init = true;
}
public Inventory(string A, string B, string C) {
_1 = A;
_2 = B;
_3 = C;
init = true;
}
public Inventory(string A, string B) {
_1 = A;
_2 = B;
init = true;
}
public string Index(int i) {
switch(i) {
case 1:
return this._1;
case 2:
return this._2;
case 3:
return this._3;
case 4:
return this._4;
default:
return "";
}
}
public void SetIndex(int i,string set) {
switch (i) {
case 1:
_1 = set;
return;
case 2:
_2 = set;
return;
case 3:
_3 = set;
return;
case 4:
_4 = set;
return;
default:
return;
}
}
public Inventory(string A) {
_1 = A;
init = true;
}
public int Length() {
int length = 0;
for (int i = 0; i < 4; i++) {
if(Index(i+1)!="") length++;
}
return length;
}
public Inventory Format() {
Inventory outt = this;
bool Formatted = true;
for(int i = 0; i< 4; i++) {
if(Index(i+2)==""&&Index(i+3)=="") {
Formatted = false;
break;
} else {
Formatted = true;
}
}
for(int i = 0; i < 4; i++) {
if(Index(i+1)=="") {
if (i != 3) {
outt.SetIndex(i + 1, outt.Index(i + 2));
outt.SetIndex(i + 2, "");
}
}
}
return !Formatted ? outt.Format() : outt;
}
public int FullLength = 4;
public static Inventory operator +(Inventory a,string b) {
//Get length
a = a.Format(); //Error here
Debug.Log(a.Length()); //If I comment out first line, this gets error too
int l = a.Length();
if(l==4) {
return a;
}
switch(l) {
case 0:
return new Inventory(b);
case 1:
return new Inventory(a._1, b);
case 2:
return new Inventory(a._1, a._2, b);
case 3:
return new Inventory(a._1, a._2, a._3, b);
}
return a;
}
public static Inventory operator -(Inventory a,string b) {
a = a.Format();
Inventory A = a;
for(var i=0;i<4;i++) {
if(A.Index(i+1)==b) {
A.SetIndex(i + 1,"");
break;
}
}
A = A.Format();
return A;
}
}
这是我称之为+运算符的区域:
public class Inevntory : MonoBehaviour {
private Inventory Inv;
public GameObject Crosshair;
public float Radius;
// Start is called before the first frame update
void Start() {
Crosshair.SetActive(false);
}
// Update is called once per frame
void Update() {
Transform transform1 = gameObject.transform;
//Take items
TakeItems(transform1);
}
void TakeItems(Transform transform1) {
GameObject[] objs = GameObject.FindGameObjectsWithTag("Item");
bool Rad = false;
GameObject obj = gameObject; //Give object dummy value to pass compiler
foreach (GameObject i in objs) {
if ((transform1.position - i.transform.position).magnitude < Radius) {
Rad = true;
obj = i;
}
}
Crosshair.SetActive(Rad);
if (Rad) {
//If there is an item within radius
if (Input.GetButtonDown("Fire2")) {
Inv = Inv + obj.name;
obj.SetActive(false);
}
}
}
}
NullReferenceException:对象引用未设置为对象的实例 Inventory.op_Addition(库存a,系统字符串b)(在Assets / Scripts / Inevntory.cs:112) Inevntory.TakeItems(UnityEngine.Transform transform1)(位于Assets / Scripts / Inevntory.cs:176) Inevntory.Update()(位于Assets / Scripts / Inevntory.cs:158)