How to create an in-game search engine ? Also, how to select the object?

Hi Everyone,
I hope you all are doing well. I was wondering if someone could explain how to create a search function within my game. Also, how to select and highlight the game object that is been searched for? For example, let’s say my game includes my home. I would like to search for the Master Bedroom (GameObject) in a search bar. As I press enter, I would like it to highlight my Master Bedroom and slightly hide the rest of the rooms. I hope this makes sense. I really would appreciate someone’s help. Thank you

What part are you having trouble with?

  1. When clicking searchbar, create array/list of all gameobjects in scene.
    You can use one of these:
    GameObject.FindObjectsOfType(typeof(MonoBehaviour)); //returns Object[ ]
    GameObject.FindGameObjectsWithTag(“Untagged”); //returns GameObject[ ]

  2. Now when you finish typing text, use a foreach:
    foreach (GameObject searchedGO in PreviouslyMadeArray)
    {
    (if searchedGO.name == enteredText);
    // perform code
    }

This falls under “Find Element In Array” which is a classic homework assignment for beginner programmer. The algorithm is as follows:

* for each element in array:
** If the element matches search criteria
***Add it to result list.

And that’s it.

FindObjectsOfType are not exactly fast, and it will be likely much faster to searc through transform children of a specific node.Or just store list of searchable objects in the first place.

1 Like

Also
“If object is in search results, highlight, otherwise don’t highlight”.