- Home /
Search GameObjects while in game
I would like to add a in game search engine that searches for gameobjects and selects the gameobject using its' collider.
Take my simple example with four blocks
Lets say I add a Input Text Field and a search button like this:
Currently I have a highlight gameobject script and also this upcoming script : which displays the gameobjects name on screen once selected.
using UnityEngine;
using System.Collections;
public class Cube : MonoBehaviour
{
public GUISkin Border;
float native_width = 720;
float native_height = 1280;
public bool CubeInfo = false;
public GUIStyle style;
string Cube_Name = "".PadLeft(100);
public bool Cube_Buttons = false;
private Rect NameRect;
void Start()
{
Cube_Name = gameObject.name;
SetGUIAspectRatio();
}
void OnMouseDown()
{
Display();
}
//Display Object Name
private void Display()
{
Cube[] Cubes = FindObjectsOfType(typeof(Cube)) as Cube[];
foreach (Cube o in Cubes)
{
o.Cube_Buttons = false;
}
Cube_Buttons = true;
}
//Sets the Aspect Ratio of GUI
void SetGUIAspectRatio()
{
NameRect = new Rect(75, 0, Screen.height, 75);
}
void OnGUI()
{
float rx = Screen.width / native_width;
float ry = Screen.height / native_height;
GUI.matrix = Matrix4x4.TRS(new Vector3(0f, 0f, 0f), Quaternion.identity, new Vector3(rx, ry, 1f));
if (Cube_Buttons)
{
// Make a Name Button
GUI.Button(NameRect, Cube_Name.PadRight(50), style);
}
}
}
Using C# what would I need in a script that if I type "Yellow" in the Input Text field and click select, the Yellow box is selected and the name is displayed using the same script as listed above. Since I am just starting this off, I think a complete answer from start to finish is to much to ask for without providing preexisting scripts to piggyback off of. A Tutorial, or a point in the right direction with a link would be much appreciated
Answer by Bunny83 · Jun 23, 2016 at 10:42 PM
Well, this is much simpler as you might think, however the biggest open question is what you understand by "select" / "selected".
To actually find a gameobject be name you can simply use GameObject.Find(). It will return the reference to the gameobject with the entered name if one exists. Again the open question is how you want to represent the "selection" visually.
I updated my question, hopefully that makes it more clear.
Your answer
![](https://koobas.hobune.stream/wayback/20220612083543im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Destroying Game Object On MouseButtonDown + Distance Collision 0 Answers
I need help with triggers 1 Answer
Simple In game search field 2 Answers
Array of colliders/triggers 0 Answers