- Home /
Search bar in runtime? Linking the result of search to gameobject and changing the color of gameobject ?
Hello everyone, I'm newbie to Unity. I'm trying to create a search bar in game view Unity. Following this topic,
http://answers.unity3d.com/questions/643598/xeong-question-pt-2-search-bar-issue.html
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.
Answer by vantrang · Jul 26, 2016 at 09:52 AM
Finally , I found the solution. Here's the script:
if (GUI.Button(new Rect(buttonPosX, buttonPosY, GOtransform.name.Length*6+2, 30), GOtransform.name))
{
GOtransform.transform.gameObject.GetComponent<Renderer>().material.color=Color.red;
}
But I have another problem: How I can make the others (gameobjects) semi transparent? It means when i select a room, only the selection change the color to red and the others will be semi transparent? As I know that they are all childs of game object "RoomDyn3DMax" in this image:
In the game view, by using search bar, I click on the button of a room, this room change the color to red and the others room will be semi transparent?
Thanks in advance.
Your answer
Follow this Question
Related Questions
Android: Search button event 0 Answers
Searching buttons with inputfield. 1 Answer
Search button filter 1 Answer
Toggle between animation states with UI button 1 Answer
How can I make GUI button send an input message when I press it? 0 Answers