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 birns92 · Jun 24, 2016 at 08:12 AM · gameobjectcolliderdestroyfindsearch

Simple In game search field

I have a few game objects that each have colliders. When I click the game object the console prints its name. Here is the script I have attached to each game object.

 public void Start ()
     {
         string name = gameObject.name;
     }
 
     public void OnMouseDown()
     {
         Debug.Log(name);
     }

I also have a input field and a button that I currently can type in the name of a game object and destroy it.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityEditor;
 
 public class Search : MonoBehaviour
 {    
     public Cube cube;
     public InputField Name;
 
     public void start ()
     {        
         GameObject selected = GameObject.Find (Name.text);
         Destroy (selected);
     }
 }

Instead of Destroying the gameobject in the second script, how can I instead select the game objects collider as if I used OnMouseDown() from the first script; even more specifically How can I search the game object in the input field and "select" the gameobject as if I simple clicked on it by hand. This is what I am looking for:

      public void start ()
          {        
              GameObject selected = GameObject.Find (Name.text);
              Activate (selected); //activate the mesh collider to trigger     
                                                //OnMouseDown from first script. 
                                                //I know this  won't work, but I am not 
                                                // sure what the proper syntax is here.
          }









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

2 Replies

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

Answer by Eudaimonium · Jun 24, 2016 at 09:25 AM

Make a script that you will attach to your selectable objects, and have a function in it for selecting it, for example:

 //script is, say, Selectable.cs
 
 public void Select()
 {
   //Do fancy stuff here - apply new graphics for selection, etc
 }

Then, in your method where you fetch the input name:

 public void Start()
 {
       GameObject selected = GameObject.Find (Name.text);
     selected.GetComponent<Selectable>().Select();
 }

The fancy part is fetching the component (script) of the targeted game object and calling it's predefined function.

Note: I noticed you called your method "start". If you wish it to execute at the start of the game, it needs to be called "Start".

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
avatar image
0

Answer by ETech-Studios · Jun 24, 2016 at 04:11 PM

The name string is a local variable. This means it can only be read / written by code in the method in which it was declared. You need to declare it oustide of any methods.

 string name;
 
 public void Start ()
      {
          name = gameObject.name;
      }
  
 public void OnMouseDown()
      {
          Debug.Log(name);
      }

Also you can call OnMouseDown() from another script.

       public void start ()
           {        
               GameObject selected = GameObject.Find (Name.text);
               selected.OnMouseDown();
           }
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

66 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Locate a game object using rays 1 Answer

destroy gameobject when collider is fast enough? 2 Answers

Find Keyword in a Searched GameObject (JS) 2 Answers

Finding Assets/Prefabs That Are Not In The Scene 1 Answer


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