Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Find a gameobject in project windows (through code)

Discussion in 'Scripting' started by sirio21, Sep 1, 2016.

  1. sirio21

    sirio21

    Joined:
    Mar 11, 2013
    Posts:
    114
    hi!! i need to locate a gameobject in project windows programatically.
    Exactly like if we use the search window in the project tab.

    It's possible to do this?
    Thanks!!
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I'm assuming we're talking some sort of editor script?
     
  3. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    Maybe you can use Resources.FindObjectsOfTypeAll() to search for Objects, then iterate through the array to seach by name. This is going to be very slow, though, so hopefully it isn't intended for gameplay. I think it returns scene objects, too.
     
  4. sirio21

    sirio21

    Joined:
    Mar 11, 2013
    Posts:
    114
    i need to do it in editor not at run time; in a lot of gameobject having the name with a button i need to bring it (locate it) quickly; actually i need to take the name, go to project windows and type the name it to locate, i need to locate automatically with code
     
  5. sirio21

    sirio21

    Joined:
    Mar 11, 2013
    Posts:
    114
    plis plis
     
  6. skalev

    skalev

    Joined:
    Feb 16, 2012
    Posts:
    264
    Like @Hyblademin said, use Resource.Resources.FindObjectsOfTypeAll() with the desired type. This will give you a list of all objects in the scene and project pane that are of that type. You then will have to take the array you are given and filter out the results based on your search. If you get a list of GameObjects, you can access the .scene property and check if that is null, which means the object isn't in the scene. You can use the Selection class to select the objects you have come up with.
     
  7. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
  8. skalev

    skalev

    Joined:
    Feb 16, 2012
    Posts:
    264
    Actually that is completely wrong.

    Execute in edit mode is meant to have a regular script ALSO execute while in edit mode, aside from play mode.

    In order to make editor scripts, you'd like to use editor classes ( Editor, EditorWindow) or use a scriptable object.
     
  9. sirio21

    sirio21

    Joined:
    Mar 11, 2013
    Posts:
    114
    My problem is how i can interact with the project window (in order to show it there) when game object is found it.
     
  10. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    Completely wrong seems a little harsh... I'd say mostly wrong, but only due to semantics. Anyway, what I meant to say was, "try ExecuteInEditMode". I'm not sure what exactly what OP's trying to do, so I don't know if that's going to cut it for them.

    Actually, can you give us a better idea of what you're doing @sirio21 ? I don't really know what you mean by "interact with the project window"; are you trying to highlight things?
     
  11. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
  12. skalev

    skalev

    Joined:
    Feb 16, 2012
    Posts:
    264
    Might be a little harsh :) point is, ExecuteInEditMode is not for making editor scripts, but rather have gameplay scripts also run in editor mode outside of play mode. This is an important distinction, especially with this case, where the OP wants to script the project window. For what he is attempting to do, which from my understanding is to basically script the search functionality of Project Window, he'll need an editor script, and the UnityEditor namespace as well, both are unavailable in a built gameplay script. I wanted to make this distinction especially for future readers.

    Back to the problem at hand though, you are going to have to code this up by yourself. You can't really interact with the project window natively (without some magic). Like mentioned already, if you get the proper object, and then call Selection.ActiveObject = theObject you just found, it will select it in the project window.
     
  13. sirio21

    sirio21

    Joined:
    Mar 11, 2013
    Posts:
    114
    Sorry for my english,,
    Having the (blue) Name of gameobject (a material in this case), i need to locate and show it (green) like if i type the name (red) in the project window search texboxt. Idea is create a button (blue) with the name of gameobject to locate it quickly.