自定义列表 .Remove
Custom List .Remove
嘿嘿。首先,如果我问的问题非常简单,我很抱歉。它已经让我耽搁了一两天,我找不到任何教程来涵盖我正在尝试做的事情。
我有一组必须容纳一些游戏对象的立方体,以及一个显示该对象的重要性以及它将出现在称为解决顺序的东西中的位置的整数。那位一切正常。
CubeContents class:
public class CubeContents : IComparable<CubeContents>{
public GameObject objectType;
public int resolutionOrder;
public CubeContents (GameObject name, int importance){
objectType = name;
resolutionOrder = importance;
}
public int CompareTo(CubeContents other){
if(other == null)
return 1;
return resolutionOrder - other.resolutionOrder;
}
我正在使用的方法将特定游戏对象传递给立方体并使用此代码将其添加到数组中:
public void newArrival(GameObject incoming){
int importance = discoverImportanceOfObject (incoming);
thisContents.Add (new CubeContents (incoming, importance));
thisContents.Sort ();
}
discoverImportanceOfObject 基本上是一长串 "else if" 语句,returns 我用来订购这些东西的数字。我遇到的问题是当我试图在销毁它之前从数组中删除该对象时。这段代码基本上看起来完全没有响应,但它编译和运行得很好。没有奇怪的错误消息,什么都没有。
public void leavingObject(GameObject leaving){
int importance = discoverImportanceOfObject (leaving);
thisContents.Remove (new CubeContents (leaving, importance)));
thisContents.TrimExcess ();
}
我完全不知道这是为什么。我尝试了各种方法(IndexOf,然后是 RemoveAt,清空整个数组,然后根据碰撞器重建它...)
感觉这将是一个我完全忽略的简单修复,但因为我没有要搜索的错误消息或任何其他类型出发点,我有点卡住了...
这个对象将不存在...
thisContents.Remove (new CubeContents (leaving, importance)));
而是循环遍历 thisContents 集合以查找与 'leaving' 和 'importance' 匹配的对象。然后删除该对象。
嘿嘿。首先,如果我问的问题非常简单,我很抱歉。它已经让我耽搁了一两天,我找不到任何教程来涵盖我正在尝试做的事情。
我有一组必须容纳一些游戏对象的立方体,以及一个显示该对象的重要性以及它将出现在称为解决顺序的东西中的位置的整数。那位一切正常。
CubeContents class:
public class CubeContents : IComparable<CubeContents>{
public GameObject objectType;
public int resolutionOrder;
public CubeContents (GameObject name, int importance){
objectType = name;
resolutionOrder = importance;
}
public int CompareTo(CubeContents other){
if(other == null)
return 1;
return resolutionOrder - other.resolutionOrder;
}
我正在使用的方法将特定游戏对象传递给立方体并使用此代码将其添加到数组中:
public void newArrival(GameObject incoming){
int importance = discoverImportanceOfObject (incoming);
thisContents.Add (new CubeContents (incoming, importance));
thisContents.Sort ();
}
discoverImportanceOfObject 基本上是一长串 "else if" 语句,returns 我用来订购这些东西的数字。我遇到的问题是当我试图在销毁它之前从数组中删除该对象时。这段代码基本上看起来完全没有响应,但它编译和运行得很好。没有奇怪的错误消息,什么都没有。
public void leavingObject(GameObject leaving){
int importance = discoverImportanceOfObject (leaving);
thisContents.Remove (new CubeContents (leaving, importance)));
thisContents.TrimExcess ();
}
我完全不知道这是为什么。我尝试了各种方法(IndexOf,然后是 RemoveAt,清空整个数组,然后根据碰撞器重建它...)
感觉这将是一个我完全忽略的简单修复,但因为我没有要搜索的错误消息或任何其他类型出发点,我有点卡住了...
这个对象将不存在...
thisContents.Remove (new CubeContents (leaving, importance)));
而是循环遍历 thisContents 集合以查找与 'leaving' 和 'importance' 匹配的对象。然后删除该对象。