Find an object by a part of its name

Hi,

I have many objects with different names

buidling01
buidling02
buidling01_window02
buidling01_window01
buidling02_window01
and so on…

To find an object with full name, i have this simple script:

using UnityEngine;
using System.Collections;



public class ExampleClass : MonoBehaviour
{
    public GameObject myObjectName;

    void Example()
    {
      
        myObjectName = GameObject.Find("buidling01_window02");

    
    }
}

But how do I change the script to find only a part of the name, for example “window01” oder “buidling02”?

Why do you need to find the objects? I would think of a different approach as find with string is slow. You could have a class which stores a reference to the building parts.

ok… how could that actually work?

Take a look at the first example for Transform: You can iterate over all children of this game object. So just put all game objects under a parent one and iterate once(!) over them and remember all necessary information for your task.

If all your building objects are tagged the same, you could have two fields in each object called name1 and name2 and set the name1 and name2 fields as the same as the first and second parts of their name. Then do something like this:

gameObjects = GameObject.FindGameObjectsWithTag("Building");

Then iterate through gameObjects and check for your search criteria in both name1 and name2 fields.

Before you go down that partial route, what are you ging to do with the OTHER objects that match the partial Name, or is that the Goal, to find all objects whose name have the same substring?

My goal is: I want to make them visible or invisible in different combinations.