Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by vantrang · Jul 26, 2016 at 07:39 AM · buttonsearchgameobject.findgameobject

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 :() alt text 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.

capture.png (105.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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: alt text

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.


picture.png (13.0 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges