- Home /
How to modify specific GameObjects in an array of GameObjects?
I want to "select" specific gameObjects in an array of gameObjects. In my case specifically, I want to modify all the gameObjects in the array, excluding the first gameObject (gameobject[0]).
Is there a way to somehow specify gameObject[1 - entire array of gameObjects after 1]
? I suppose I would compare it to a range of integers that specify specific positions in the array (1 - gameObjectArray.Length) or something like that.
This is a working solution to my problem, but you should probably resubmit this as an answer ins$$anonymous$$d of a comment. Thanks!
Answer by dmg0600 · Sep 23, 2014 at 09:38 AM
for (int i = 1; i < gameObjectArray.Length; ++i)
{
gameObjectArray[i] // Do your modification here
}
If you want to exclude more elements or make it more general:
List<int> elementsToExclude = new List<int>(new int[] {0,4,7});
for (int i = 0; i < gameObjectArray.Length; ++i)
{
if (elementsToExclude.Contains(i))
continue;
gameObjectArray[i] // Do your modifications here
}
Great! This works, I just had to remember that in the List we are specifying which gameObjects to exclude not which gameObjects to include in the modifications.
Thanks so much!