C# SetActive的反应不同
根据输入字符串,我想在脚本中激活我的游戏对象。我的游戏对象拥有一个公共字符串,我将其与给定数组的值进行比较:C# SetActive的反应不同,c#,unity3d,multidimensional-array,gameobject,C#,Unity3d,Multidimensional Array,Gameobject,根据输入字符串,我想在脚本中激活我的游戏对象。我的游戏对象拥有一个公共字符串,我将其与给定数组的值进行比较: GameObject parent = GameObject.Find("Pick Ups"); int childCount = parent.transform.childCount; foreach (var s in arr) { for (int i = 0; i < childCount; ++i) { // Deactivate Target if i
GameObject parent = GameObject.Find("Pick Ups");
int childCount = parent.transform.childCount;
foreach (var s in arr)
{
for (int i = 0; i < childCount; ++i)
{
// Deactivate Target if it is not part of the Active Objects List
if (s == parent.transform.GetChild(i).GetComponent<Rotator>().targetID)
{
parent.transform.GetChild(i).gameObject.SetActive(true);
Debug.Log("Element " + s + " activated");
} else {
parent.transform.GetChild(i).gameObject.SetActive(false);
}
}
}
GameObject父对象=GameObject.Find(“拾取”);
int childCount=parent.transform.childCount;
foreach(arr中的var s)
{
对于(int i=0;i
这很好用。现在,我增加了为输入获取的信息量,将其存储在一个三维数组中,并循环抛出包含与以前相同信息的第一个维度:
var tArr = aTLString.Split('~')
.Select(x => x.Split('^')
.ToArray()
.Select(y => y.Split('`'))
.ToArray())
.ToArray();
for (int j = 0; j < tArr.GetLength(0); ++j)
{
for (int i = 0; i < childCount; ++i)
{
// Deactivate Target if it is not part of the Active Objects List
if (tArr[j][0][0] == parent.transform.GetChild(i).GetComponent<Rotator>().targetID)
{
parent.transform.GetChild(i).gameObject.SetActive(true);
Debug.Log("Element " + tArr[j][0][0] + " activated");
} else {
parent.transform.GetChild(i).gameObject.SetActive(false);
}
}
}
var tArr=aTLString.Split(“~”)
.Select(x=>x.Split(“^”)
.ToArray()
.Select(y=>y.Split('`'))
.ToArray())
.ToArray();
对于(int j=0;j
这一次,该元件未激活。我得到控制台反馈,元素被激活(因此tArr[j][0][0]包含正确的字符串),但SetActivate似乎不起作用
我不明白这个问题在
tArr
的每次迭代中,您都在所有对象上调用SetActive
。即使对象在第一个过程中匹配,它也会在后续过程中停用,仅使lass过程有效 您的算法相当于(试图使其尽可能简洁): 这将产生:
1 active: False
2 active: True
3 active: False
1 active: False
2 active: False
3 active: False
1 active: False
2 active: False
3 active: False
您可以看到,即使在targetId
的第一次迭代中匹配了ID 2,它也会在后续过程中重置
您可能要做的是首先将所有对象设置为非活动状态,然后迭代tArr
并激活匹配的对象,但不要停用不匹配的对象
或者,如果对象的id包含在tArr
中,您可以在对象上迭代一次并激活它们
foreach (var thing in things)
{
thing.Active = targetIds.Contains(thing.Id);
}
在您的第一次测试中,
arr
中有多少元素?是1吗?arr中有2个元素,在tArrinarr
的第一维度中有2个元素,是第二个元素激活了对象吗?你是对的!但我想知道为什么它会与第一个数组一起工作……可能是因为激活您正在测试的对象的值在arr
的最后一个元素中。在tArr中也是如此。。。不管怎样,这是个愚蠢的错误。谢谢你的帮助!
foreach (var thing in things)
{
thing.Active = targetIds.Contains(thing.Id);
}