Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'General Discussion' started by vik4christy, May 11, 2020.

  1. vik4christy

    vik4christy

    Joined:
    Mar 5, 2020
    Posts:
    1
    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
     
  2. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,083
    What part are you having trouble with?
     
  3. Charles-Brant

    Charles-Brant

    Joined:
    May 11, 2020
    Posts:
    9
    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
    }
     
  4. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,554
    This falls under "Find Element In Array" which is a classic homework assignment for beginner programmer. The algorithm is as follows:
    Code (csharp):
    1.  
    2. * for each element in array:
    3. ** If the element matches search criteria
    4. ***Add it to result list.
    5.  
    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.
     
    Ryiah likes this.
  5. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,554
    Also
    "If object is in search results, highlight, otherwise don't highlight".