Selecting a sequence of game objects with Gameobject.Find

Hi there,

I have a bunch imported FBX objects that are named in sequence. Like this → Object_01,Object_02 - Object_10 and I was wondering if there’s a way I could select them all by name (like in Gameobject.Find) so I can add them sequentially to an array.

Any ideas?
Thanks
Pete.

Use this code to find all Objects in Group

using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
private ArrayList allObjectList=new ArrayList();
private Transform[ ] myObject;
void Awake() {
myObject = GetComponentsInChildren();
foreach (Transform obj in myObject) {
allObjectList.Add(obj.gameObject);
}
print (allObjectList.Count);

}

}

  1. Don’t use ArrayList.
  2. That’s not what the OP asked for.

You can either tag them all the same and use FindGameObjectsByTag or if the naming convention is consistent you can do something like this.

List<GameObject> objList = new List<GameObject>();
GameObject theObject = null;
int i = 0;
string baseName = "Object_";
do
{
    if (i < 10)
    {
        theObject = GameObject.Find(baseName + "0" + i);
    }
    else
    {
        theObject = GameObject.Find(baseName + i);
    }

    if (theObject) objList.Add(theObject);

} while (theObject);

Hey Thanks KelsoMRK,

That’s more like what I was looking for, but what if you don’t know what the end of a name might be? Is there a way of ignoring it?
In my case the objects are named like this - “Character_G_Hat_01_Straw, Character_G_Hat_02_Cowboy, Character_G_Hat_03_TopHat”
So it works up until the object description i.e. TopHat. I’m currently putting them into arrays manually, which is okay but i was hoping there’d be a nice way to automate it.

Thanks
Pete

Are these objects part of the scene view or are they the .fbx files in the project view? It sounds to me like you want to preload assets and create an array of GameObjects out of them?

Oh sorry, I’m importing a .fbx of each character with all of it’s elements, and then sorting them out within a scene. So I’m going through and manually putting each element into an array, where I feel as though I should be able to sort them based on the naming convention.

Sorry, I guess I’m a little confused as to what you are doing. Are you putting the prefab into the scene and you want to have an array within a MonoBehaviour that has a sorted list of the sub-meshes of the fbx?

No, I’m just trying to find a way to add objects to an array based on their names.

The objects are named like this,
“Character_G_Hat_01_Straw, Character_G_Hat_02_Cowboy, Character_G_Hat_03_TopHat”

Say I have an array for the hats, I’d just like it to go through the scene and find anything that’s named Character_G_Hat_0* and add it to that array.

Thanks,
Pete

Oh, okay. Thanks for the details. So in this instance, you want to build a list of “Hats”, correct? Are these objects already on a parent transform?

Example:
Character_G
|–> Character_G_Hat_01_Straw
|–> Character_G_Hat_02_Cowboy
|–> Character_G_Hat_03_TopHat

Yes they are all parented to the character.