C# 如何从列表中删除特定的游戏对象(在数组中)?

C# 如何从列表中删除特定的游戏对象(在数组中)?,c#,arrays,list,unity3d,C#,Arrays,List,Unity3d,我的代码目标是让多个头骨出现在屏幕上进行比较。根据屏幕上头骨的数量(1-6),它们会改变位置和大小,我通过使用一系列游戏对象(头骨)和一个可以跟踪头骨处于哪个“位置”的列表来做到这一点 如果正在添加,则代码可以工作,但在删除对象时会遇到问题。只有当你按照添加游戏对象的相同顺序删除游戏对象时,它才会在RemoveAt上“起作用”。IE:列表总是删除元素0(列表中的第一项),而不是启动RemoveAt功能的特定游戏对象 虽然我已经找到了很多关于如何移除特定单个游戏对象的答案,但它们都不起作用,因为我

我的代码目标是让多个头骨出现在屏幕上进行比较。根据屏幕上头骨的数量(1-6),它们会改变位置和大小,我通过使用一系列游戏对象(头骨)和一个可以跟踪头骨处于哪个“位置”的列表来做到这一点

如果正在添加,则代码可以工作,但在删除对象时会遇到问题。只有当你按照添加游戏对象的相同顺序删除游戏对象时,它才会在RemoveAt上“起作用”。IE:列表总是删除元素0(列表中的第一项),而不是启动RemoveAt功能的特定游戏对象

虽然我已经找到了很多关于如何移除特定单个游戏对象的答案,但它们都不起作用,因为我的游戏对象在一个数组中——我不能简单地说移除游戏对象名称。这就引出了我的问题:如何从列表中删除特定的游戏对象[]

remove或removeAll运行以下两个错误:

错误CS1502:
System.Collections.Generic.List.RemoveAll(System.Predicate)”的最佳重载方法匹配具有一些无效参数。
错误CS1503:参数
#1'无法将
int'表达式转换为类型
System.Predicate'

以下是我的两个脚本:

每个名为birdController的鸟头骨上的脚本:

使用名为cubePlacement的所有数组和列表信息创建空脚本:

public int numberSkullOnScreen;
公共场所[]臭鼬;
公共游戏对象[]所有头骨;
公共游戏对象列表持有者;
public List birdsOnScreen=新建列表();
//声明脚本以避免在引用它的位置使用NullReferenceEcxeption
公共鸟类控制器(birdController);;
//用于初始化
无效开始(){
//使列表正常工作的唯一方法是:在启动时让一个对象充当占位符
屏幕上的鸟。添加(列表持有者);
}
//打开和关闭头骨
公共无效切换birdskull(int mySkull){
if(skullOn[mySkull]==false){
//将特定游戏对象设置为活动状态
所有头骨[mySkull].SetActive(true);
//把骷髅地变成真的
skullOn[mySkull]=真;
//在屏幕上的数字中添加一个
numberSkullOnScreen++;
//从Bird控制器脚本中引用int orderInList
allSkulls[mySkull].gameObject.GetComponent().orderInList=numberSkullOnScreen;
//打开此选项后,将骷髅添加到列表
添加(所有头骨[mySkull]);
//运行函数以放置头骨
放置颅骨();
}
否则{
所有头骨[mySkull].SetActive(false);
skullOn[mySkull]=假;
数字KullonScreen--;
//关闭时从列表中删除骷髅-这不起作用。。。
屏幕上的birdsOnScreen.RemoveAt(所有头骨[mySkull].gameObject.GetComponent().orderInList);
//运行函数以根据屏幕上的整数头骨放置头骨
放置颅骨();
}
}

我想您正在寻找类似于列表中的函数。你可以用这个:

public static void RemoveAt<T>(ref T[] arr, int index) {
        for (int a = index; a < arr.Length - 1; a++)
        {
            arr[a] = arr[a + 1];
        }
        Array.Resize(ref arr, arr.Length - 1);
    }
publicstaticvoidremoveat(参考T[]arr,int索引){
for(int a=索引;a
或者,如果您知道该对象:

public static T[] RemoveObjectArray<T> (this T[] arr, T ObjToRemove) {  
        int numIdx = System.Array.IndexOf(arr, ObjToRemove);
        if (numIdx == -1) return arr;
        List<T> tmp = new List<T>(arr);
        tmp.RemoveAt(numIdx);
        return tmp.ToArray();
    }
public static T[]RemoveObjectArray(此T[]arr,T ObjToRemove){
int numIdx=System.Array.IndexOf(arr,ObjToRemove);
if(numIdx==-1)返回arr;
列表tmp=新列表(arr);
tmp.RemoveAt(numIdx);
返回tmp.ToArray();
}

我想您正在寻找类似于列表中的函数。你可以用这个:

public static void RemoveAt<T>(ref T[] arr, int index) {
        for (int a = index; a < arr.Length - 1; a++)
        {
            arr[a] = arr[a + 1];
        }
        Array.Resize(ref arr, arr.Length - 1);
    }
publicstaticvoidremoveat(参考T[]arr,int索引){
for(int a=索引;a
或者,如果您知道该对象:

public static T[] RemoveObjectArray<T> (this T[] arr, T ObjToRemove) {  
        int numIdx = System.Array.IndexOf(arr, ObjToRemove);
        if (numIdx == -1) return arr;
        List<T> tmp = new List<T>(arr);
        tmp.RemoveAt(numIdx);
        return tmp.ToArray();
    }
public static T[]RemoveObjectArray(此T[]arr,T ObjToRemove){
int numIdx=System.Array.IndexOf(arr,ObjToRemove);
if(numIdx==-1)返回arr;
列表tmp=新列表(arr);
tmp.RemoveAt(numIdx);
返回tmp.ToArray();
}

您是否尝试使用
remove
方法删除对象? 像这样:

birdsOnScreen.Remove (allSkulls[mySkull]);
你的
birds在屏幕上。RemoveAt
版本看起来也正确。 如果要使用
RemoveAll
方法,应该传递谓词,该谓词返回
bool
,例如:

birdsOnScreen.RemoveAll (bird => { return bird == allSkulls[mySkull]; } );
我建议在您的情况下使用
Remove
,因为您知道要删除的对象

请在此处阅读更多信息:

另外,在将其值指定给对象的属性后,尝试增加您的
numberSkullOnScreen

allSkulls[mySkull].gameObject.GetComponent<birdController>().orderInList = numberSkullOnScreen;

numberSkullOnScreen++;
allSkulls[mySkull].gameObject.GetComponent().orderInList=numberSkullOnScreen;
numberSkullOnScreen++;

这样,您就不需要再使用占位符了,因为您的索引将是正确的。

您是否尝试过使用
remove
方法删除对象? 像这样:

birdsOnScreen.Remove (allSkulls[mySkull]);
你的
birds在屏幕上。RemoveAt
版本看起来也正确。 如果要使用
RemoveAll
方法,应该传递谓词,该谓词返回
bool
,例如:

birdsOnScreen.RemoveAll (bird => { return bird == allSkulls[mySkull]; } );
我建议在您的情况下使用
Remove
,因为您知道要删除的对象

请在此处阅读更多信息:

另外,在将其值指定给对象的属性后,尝试增加您的
numberSkullOnScreen

allSkulls[mySkull].gameObject.GetComponent<birdController>().orderInList = numberSkullOnScreen;

numberSkullOnScreen++;
allSkulls[mySkull].gameObject.GetComponent().orderInList=numberSkullOnScreen;
numberSkullOnScreen++;

这样,您就不需要再使用占位符了,因为您的索引将是正确的。

嗨,Sarah,欢迎来到Stack overflow。我建议你编辑你的文章,以产生一个新的,以便有人能够