Hello everyone,
I’m newbie to Unity. I’m trying to create a search bar in game view Unity. Following this topic,
I’ve created a search box in the game view but i want to make a button of each result. . When I click on the button, the selected object will change the color to red.
This is the image of searching bar ( it’s not beautiful but at least it works :()
When I type “Bureau”, it will show the game objects which contain the “Bureau” in their name. So, I use GUI.Button with transform of game object to create the buttons in the image. Here’s the script:
using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEngine.UI;
public class SearchBox : MonoBehaviour {
string searchString = "Recherche";
public GameObject[] items;
GameObject firstGameObject;
Transform GOtransform;
int buttonPosX, buttonPosY;
void OnGUI()
{
GUILayout.BeginHorizontal(EditorStyles.toolbar);
GUILayout.FlexibleSpace();
searchString = GUILayout.TextField(searchString, EditorStyles.toolbarTextField);
GUILayout.EndHorizontal();
firstGameObject = items [0];
// Do comparison here. For example
if (!string.IsNullOrEmpty (searchString)) {
//Debug.Log (firstGameObject.name);
buttonPosX=10;
buttonPosY=70;
for(int i=0; i<firstGameObject.transform.childCount; i++){
GOtransform=firstGameObject.transform.GetChild(i);
if (GOtransform.name.ToUpper().Contains(searchString.ToUpper()))
{
if(GUI.Button(new Rect(buttonPosX, buttonPosY, GOtransform.name.Length*6+2, 30), GOtransform.name))
gameObject.transform.Find(string Gotransform.name).gameObject.GetComponent<Renderer>().material.color=Color.red;
buttonPosX+=0;
buttonPosY+=32;
}
}
}
}
}
I try to get gameobject from GOtransform in order to get component.material.color but it shows the error below:
Assets/Scripts/SearchBox.cs(47,92): error CS1525: Unexpected symbol Gotransform', expecting .’
So, the problem is on this line:
gameObject.transform.Find(string Gotransform.name).gameObject.GetComponent().material.color=Color.red;
Anyone can help me? Or another methode to create a searching bar and then click on the result to change color of gameobject? I’ve tried with the new UI button but I was totally lost with Onclick and Add listener.
Thanks in advance.

