我的代码目标是让多个头骨出现在屏幕上以进行比较。根据屏幕上头骨的数量 (1-6),它们会改变位置和大小,我通过使用一组游戏对象(头骨)和一个可以跟踪哪个头骨处于哪个“位置”的列表来完成此操作。
如果您正在添加,代码可以正常工作,但在您删除对象时会遇到问题。仅当您按照添加游戏对象的相同顺序删除游戏对象时,它才对 RemoveAt“起作用”。 IE:列表总是删除元素 0(列表中的第一项)而不是触发 RemoveAt 函数的特定游戏对象。
虽然我找到了很多关于如何删除特定的单个游戏对象的答案,但它们不起作用,因为我的游戏对象在一个数组中 - 我不能简单地说删除游戏对象名称。这让我想到了如何从列表中删除特定 gameObject[] 的问题?
remove 或 removeAll 运行这两个错误:
错误 CS1502: System.Collections.Generic.List<UnityEngine.GameObject>.RemoveAll(System.Predicate<UnityEngine.GameObject>)' has some invalid arguments.<br/>
Error CS1503: Argument
的最佳重载方法匹配#1' 无法转换 int' expression to type
系统谓词'
这是我的两个脚本:
每个名为 birdController 的鸟头骨上的脚本:
public int myBirdID;
public int orderInList;
//bool to keep track of object active or not
public bool whoAmIReally;
//potentially redundant; allows game object to see if hes on or not
public GameObject IAmMe;
// Use this for initialization
void Start () {
//fixingIntOrder();
}
public void OnEnable(){
//when object is on check bool
whoAmIReally = true;
Debug.Log("IM ON");
}
public void OnDisable(){
//when object is turned off uncheck bool, change list value to 0
//**this wont change actual list order or value in cubeplacement script- my hope is that it might have- likely missing a step somewhere
whoAmIReally = false;
orderInList = 0;
Debug.Log("IMOFF");
}
脚本为空,我的所有数组和列表信息名为 cubePlacement:
public int numberSkullOnScreen;
public bool[] skullOn;
public GameObject[] allSkulls;
public GameObject listHolder;
public List<GameObject> birdsOnScreen = new List<GameObject>();
//declaring the script to avoid NullReferenceEcxeption where I reference it
public birdController _birdController;
// Use this for initialization
void Start () {
//only way I can get my list to work: have an object act as a placeholder onStart
birdsOnScreen.Add(listHolder);
}
//turning on and off the skulls
public void toggleBirdSkull(int mySkull){
if (skullOn[mySkull] == false){
//set specific gameObject active
allSkulls[mySkull].SetActive(true);
//make skull on bool true
skullOn[mySkull] = true;
//add one to the number on screen
numberSkullOnScreen++;
//reference int orderInList from Bird controller script
allSkulls[mySkull].gameObject.GetComponent<birdController>().orderInList = numberSkullOnScreen;
//add skull to list when turned on THIS WORKS YAY
birdsOnScreen.Add(allSkulls[mySkull]);
//Run function to place skulls
placementSkulls();
}
else{
allSkulls[mySkull].SetActive(false);
skullOn[mySkull] = false;
numberSkullOnScreen--;
//remove skull from list when turned off-- THIS DOESNT WORK...
birdsOnScreen.RemoveAt(allSkulls[mySkull].gameObject.GetComponent<birdController>().orderInList);
//Run function to place skulls based on the int number skulls on screen
placementSkulls();
}
}
最佳答案
您是否尝试过使用Remove
方法移除对象?
像这样:
birdsOnScreen.Remove (allSkulls[mySkull]);
您的 birdsOnScreen.RemoveAt
版本看起来也是正确的。
如果你想使用RemoveAll
方法,你应该传递谓词,它返回bool
,例如:
birdsOnScreen.RemoveAll (bird => { return bird == allSkulls[mySkull]; } );
我建议在您的情况下使用 Remove
,因为您知道要删除的对象。
此外,在将其值分配给对象的属性后,尝试增加您的 numberSkullOnScreen
:
allSkulls[mySkull].gameObject.GetComponent<birdController>().orderInList = numberSkullOnScreen;
numberSkullOnScreen++;
这样您就不需要再使用占位符,因为您的索引将是正确的。
关于c# - 如何从列表中删除特定的游戏对象(在数组中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54610970/