[英]Leaving one gameObject, deactivate others from an array
I have an array of gameObjects which I can deactivate easily with a loop.我有一组游戏对象,我可以通过循环轻松停用它们。 However I do not want to deactivate one gameObject from the lot.
但是我不想从很多中停用一个游戏对象。 So I am providing an int value which is the position of the gameObject in that array.
所以我提供了一个 int 值,它是该数组中游戏对象的 position。 How do I deactivate others except for this particular object?
除了这个特定的 object 之外,我如何停用其他人?
public GameObject[] myObjs;
int exceptionObj;
void Start()
{
exceptionObj = 2; //Position of object in the array
for(int i = 0; i<myObjs.Length; i++)
{
myObjs[i].SetActive(false);
}
}
public GameObject[] myObjs;
int exceptionObj;
void Start()
{
exceptionObj = 2; //Position of object in the array
for(int i = 0; i<myObjs.Length; i++)
{
if(exceptionObj != i)
myObjs[i].SetActive(false);
}
}
Or;或者;
public GameObject[] myObjs;
int exceptionObj;
void Start()
{
exceptionObj = 2; //Position of object in the array
for(int i = 0; i<myObjs.Length; i++)
{
if(exceptionObj == i)
continue;
myObjs[i].SetActive(false);
}
}
Or perhaps even by simply reactivating at the end;或者甚至可以通过简单地在最后重新激活;
public GameObject[] myObjs;
int exceptionObj;
void Start()
{
exceptionObj = 2; //Position of object in the array
for(int i = 0; i<myObjs.Length; i++)
{
myObjs[i].SetActive(false);
}
myObjs[exceptionObj].SetActive(true);
}
Two ways:两种方式:
With an if:如果:
exceptionObj = 2; //Position of object in the array for(int i = 0; i<myObjs.Length; i++) { if (i.= exceptionObj) // If not the exception ID myObjs[i];SetActive(false); }
Or with two loops:或者有两个循环:
exceptionObj = 2; //Position of object in the array for(int i = 0; i<exceptionObj; i++) { myObjs[i].SetActive(false); } for(int i = exceptionObj+1; i<myObjs.Length; i++) { myObjs[i].SetActive(false); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.