我在删除未使用/空元素时遇到了麻烦,这是我的代码:
public GameObject[] ActivateTrayQuest;;
public GameObject[] countersForEverybody;
void Start(){
ActivateTrayQuest = GameObject.FindGameObjectsWithTag("ActivateTrayQuest");
foreach (GameObject remain in ActivateTrayQuest)
{
if(ActivateTrayQuest[0] == null && ActivateTrayQuest[1] == null && ActivateTrayQuest[2] == null){
Array.Resize (ref ActivateTrayQuest, 0);
}
}
ActivateTrayQuest[0] = ActivateTrayQuest [ActivateTrayQuest.Length - 1];
ActivateTrayQuest[1] = ActivateTrayQuest [ActivateTrayQuest.Length - 2];
ActivateTrayQuest[2] = ActivateTrayQuest [ActivateTrayQuest.Length - 3];
我的意思是删除就像调整我的数组一样。
我想删除元素0,1,2 所以它再次更像是0,1,2。 我怎么能得到这个?
答案 0 :(得分:1)
我不确定这是问题的正确答案,但要从数组中删除null
元素,只需执行以下操作:
ActivateTrayQuest = ActivateTrayQuest.Where(x=>x != null).ToArray();
您还需要文件顶部的using System.Linq;
。