很奇怪!我的列表 returns ArgumentOutofRangeException 尝试添加时
Very weird! My list returns ArgumentOutofRangeException when trying to add
我正在为程序做一个任务列表,所以我启动了一个 List 对象。
此列表是另一个 class 的列表,其中包含根据类型(整数)执行的操作(任务)的信息。
这是一个 Host vs Client 谈话,他们都有 class (有点不同,只是我对类型的理解(客户端任务类型 0 != 主机任务类型 0))并且我的主机列表工作得很好,但是客户没有。
我的 NetObjects 列表 returns ArgumentOutOfRangeException 试图添加 class(NetObject 的)时出现异常(我将留下代码示例)。
我不知道为什么会这样,因为列表应该是无限的,当你添加时没有索引或其他东西(它说索引必须是非负的并且小于集合的大小)。
这里是错误:
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at :0)
System.ThrowHelper.ThrowArgumentOutOfRangeException () (at :0)
System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at :0)
Networking.Update () (at Assets/Scripts/Networking.cs:225)
它说了一些我不知道为什么的获取项目,因为我(在代码中)使用带有索引的任务引用(读取它)并且它什么也没发生,只有当我想添加到NetObject 到对象 class。
我将显示主机代码,因为它可以工作,而客户端不能,我不明白为什么。
我试图通过锁定来让它变得更好(这样就不会被覆盖),但从我的角度来看,除了让它变得更好之外,我所做的并没有更多。
这是创建任务的客户端部分(在 Unity 中),我使用任务是因为如果不是来自主线程,Unity 不会让游戏事物(位置、生成等)发生变化(并且我使用线程来接收和发送信息(客户端和主机之间的对话))。
for (int i = 0; i < PendingClient.Count; i++) {
if (PendingClient[i].id > -1)
lock (lockingClient) {
switch (PendingClient[i].type) {
case 0:
GameObject go = null;
if (GetObjectFromId(PendingClient[i].id, ref go)) {
go.transform.position = new Vector3(PendingClient[i].px,
PendingClient[i].py, PendingClient[i].pz);
go.transform.rotation = Quaternion.Euler(PendingClient[i].rx,
PendingClient[i].ry, PendingClient[i].rz);
} else {
Objects.Add(new NetObject(PendingClient[i].id,
PendingClient[i].objectType, PendingClient[i].name,
PendingHost[i].prefabName,
Instantiate(Resources.Load("Prefabs/" +
PendingClient[i].prefabName) as GameObject,
new Vector3(PendingClient[i].px, PendingClient[i].py,
PendingClient[i].pz), Quaternion.Euler(PendingClient[i].rx,
PendingClient[i].ry, PendingClient[i].rz))));
}
break;
}
PendingClient.RemoveAt(i);
}
}
错误出现在最后一个 else 的正下方。
有效的主机代码:
for (int i = 0; i < PendingHost.Count; i++) {
lock (lockingHost) {
if (PendingHost[i].id > -1) {
switch (PendingHost[i].type) {
case 0: // Add Object
Objects.Add(new NetObject(PendingHost[i].id, PendingHost[i].objectType, PendingHost[i].name, PendingHost[i].prefabName, Instantiate(Resources.Load("Prefabs/" + PendingHost[i].prefabName) as GameObject, Vector3.zero, Quaternion.identity)));
break;
case 1: // Change Transform
GameObject go = null;
GetObjectFromId(PendingHost[i].id, ref go);
go.transform.position = new Vector3(PendingHost[i].px, PendingHost[i].py, PendingHost[i].pz);
go.transform.rotation = Quaternion.Euler(PendingHost[i].rx, PendingHost[i].ry, PendingHost[i].rz);
break;
}
PendingHost.Remove(PendingHost[i]);
}
}
}
我不是信息工程师,但我尽力编写更好的代码。
感谢提前。
你打电话给
PendingClient.RemoveAt(i);
里面
for (int i = 0; i < PendingClient.Count; i++) {
所以 i
最终超出范围。
旁白:与其做十几次 PendingClient[i]
,不如做
var current = PendingClient[i]
前面然后使用 current
.
我正在为程序做一个任务列表,所以我启动了一个 List 对象。 此列表是另一个 class 的列表,其中包含根据类型(整数)执行的操作(任务)的信息。 这是一个 Host vs Client 谈话,他们都有 class (有点不同,只是我对类型的理解(客户端任务类型 0 != 主机任务类型 0))并且我的主机列表工作得很好,但是客户没有。
我的 NetObjects 列表 returns ArgumentOutOfRangeException 试图添加 class(NetObject 的)时出现异常(我将留下代码示例)。 我不知道为什么会这样,因为列表应该是无限的,当你添加时没有索引或其他东西(它说索引必须是非负的并且小于集合的大小)。
这里是错误:
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at :0) System.ThrowHelper.ThrowArgumentOutOfRangeException () (at :0) System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at :0) Networking.Update () (at Assets/Scripts/Networking.cs:225)
它说了一些我不知道为什么的获取项目,因为我(在代码中)使用带有索引的任务引用(读取它)并且它什么也没发生,只有当我想添加到NetObject 到对象 class。 我将显示主机代码,因为它可以工作,而客户端不能,我不明白为什么。
我试图通过锁定来让它变得更好(这样就不会被覆盖),但从我的角度来看,除了让它变得更好之外,我所做的并没有更多。
这是创建任务的客户端部分(在 Unity 中),我使用任务是因为如果不是来自主线程,Unity 不会让游戏事物(位置、生成等)发生变化(并且我使用线程来接收和发送信息(客户端和主机之间的对话))。
for (int i = 0; i < PendingClient.Count; i++) {
if (PendingClient[i].id > -1)
lock (lockingClient) {
switch (PendingClient[i].type) {
case 0:
GameObject go = null;
if (GetObjectFromId(PendingClient[i].id, ref go)) {
go.transform.position = new Vector3(PendingClient[i].px,
PendingClient[i].py, PendingClient[i].pz);
go.transform.rotation = Quaternion.Euler(PendingClient[i].rx,
PendingClient[i].ry, PendingClient[i].rz);
} else {
Objects.Add(new NetObject(PendingClient[i].id,
PendingClient[i].objectType, PendingClient[i].name,
PendingHost[i].prefabName,
Instantiate(Resources.Load("Prefabs/" +
PendingClient[i].prefabName) as GameObject,
new Vector3(PendingClient[i].px, PendingClient[i].py,
PendingClient[i].pz), Quaternion.Euler(PendingClient[i].rx,
PendingClient[i].ry, PendingClient[i].rz))));
}
break;
}
PendingClient.RemoveAt(i);
}
}
错误出现在最后一个 else 的正下方。
有效的主机代码:
for (int i = 0; i < PendingHost.Count; i++) {
lock (lockingHost) {
if (PendingHost[i].id > -1) {
switch (PendingHost[i].type) {
case 0: // Add Object
Objects.Add(new NetObject(PendingHost[i].id, PendingHost[i].objectType, PendingHost[i].name, PendingHost[i].prefabName, Instantiate(Resources.Load("Prefabs/" + PendingHost[i].prefabName) as GameObject, Vector3.zero, Quaternion.identity)));
break;
case 1: // Change Transform
GameObject go = null;
GetObjectFromId(PendingHost[i].id, ref go);
go.transform.position = new Vector3(PendingHost[i].px, PendingHost[i].py, PendingHost[i].pz);
go.transform.rotation = Quaternion.Euler(PendingHost[i].rx, PendingHost[i].ry, PendingHost[i].rz);
break;
}
PendingHost.Remove(PendingHost[i]);
}
}
}
我不是信息工程师,但我尽力编写更好的代码。
感谢提前。
你打电话给
PendingClient.RemoveAt(i);
里面
for (int i = 0; i < PendingClient.Count; i++) {
所以 i
最终超出范围。
旁白:与其做十几次 PendingClient[i]
,不如做
var current = PendingClient[i]
前面然后使用 current
.