- Home /
Build does behave different to editor
hey folks, I have like 40 gameobjects (crosses) which are tagged the same. I get them in my script using GameObject.FindGameObjectsWithTag("") and save them in a array, then deactivate them all. Then I activate the first 12 ones using a simple for-loop. Down below is my hierachy. The marked ones should be enabled. In the editor everything works quite perfectly, but when I do a build there are wrong objects activated. They are always the same. It looks like the function does not work properly.
Has anyone an idea what is causing this problem? Thanks in advance!
for (int x = 0; x < 12; x++)
{
waysidecrosses[x].SetActive(true);
}
for (int x = 0; x < 8; x++)
{
stoups[x].SetActive(true);
}
Answer by Casiell · Jan 09, 2020 at 10:54 AM
I'm like 99% sure that GameObject.FindGameObjectsWithTag does NOT guarantee the order in which those objects are returned.
You would have to sort the array yourself to be sure you are activating correct objects.
But too be perfectly honest all this sounds like a terrible way of doing things. If it's possible it's a lot better to reference those objects in the inspector. This way you can create as many arrays as you need and assign exactly those obejcts you want in them.
If that's not possible, then it would be better to create a script with some properties that would help with filtering your objects and use that instead of the order in hierarchy
Thanks for the fast answer!. I guess you mean creating a public array and assign each gameobject manually? That´s some work for 60 objects in total, but if it fixes my problem, I´ll try. Or I just rename those and check for the name in the array as well.
Answer by 123_umer · Jan 09, 2020 at 11:10 AM
@NekrosArts i think the best approach would be is to get the reference of the parent gameobject and then loop through the children. Just give the tag to the parent object and then simply iterate through the children like this
var parent=GameObject.FindGameObjectWithTag("")
for(int i=0; i<parent.transform.ChildCount; i++){
GameObject child= parent.transform,GetChild(i);
//you can simply get each child like this and save it in some gamobject array or list
}