Search Unity

how to find all objects with certain script attached to it

Discussion in 'Getting Started' started by skeleton-king, Apr 13, 2015.

  1. skeleton-king

    skeleton-king

    Joined:
    Nov 10, 2014
    Posts:
    63
    I have a scene with lots of game objects. I wana find objects which has script named controller attached to it.
    Anyway to do it?
     
    jcameron47, Icyflames and Zeriver like this.
  2. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    open the scene you want to search.
    find the 'controller' script in your scripts folder, if you right click that script and select 'find references in scene' this will show/filter all the gameobjects that are currently in your heirarchy that has this script attached.

    if any objects are instantiated at runtime, then you will have to play then pause, and do the same search to find them.
     
  3. skeleton-king

    skeleton-king

    Joined:
    Nov 10, 2014
    Posts:
    63
    No i wana do this in the game.
    Heres my code

    public Controller sf;
    // Use this for initialization
    void Start () {

    sf= (Controller) GameObject.FindObjectOfType (typeof(Controller));

    Debug.Log(sf);

    This seems to find 2nd object in heirarchy which has controller script. How do i find 1st object in heirarchy with script.
     
  4. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Ah sorry.
    not sure, i always assumed heirarchy order wasnt relavent unless dealing with render order of the new UI Canvas.


    i just done this to find all entries and display their gameobject names, ill paste it in seen as ive done it now :)

    Code (CSharp):
    1.   Controller[] myItems = FindObjectsOfType(typeof(Controller)) as Controller[];
    2.             Debug.Log ("Found " + myItems.Length + " instances with this script attached");
    3.             foreach(Controller item in myItems)
    4.             {
    5.                 Debug.Log(item.gameObject.name);
    6.             }
     
  5. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,262
    This isn't working for me. I'm getting an error because a script that does not belong in this scene appears to be attached to an object in the scene. There's no error to fix in the script, it just shouldn't be there. I cannot find the object the incorrect script is attached to. When I do 'find references in scene,' it shows hundreds of objects, most of which have no script attached, and none of which have the script I'm looking for. How can I find the object in the scene that has the script that's causing the error?
     
    kasym_ and Misukitt like this.
  6. shadowpuppet

    shadowpuppet

    Joined:
    May 7, 2015
    Posts:
    23
    Any solutions to this? I have a script in game but no clue as to where I put it. right clicking and "find references" is totally useless as it shows pretty much everything I have in the game. tried commenting out code to "break" it because sometimes unity - when you click on the error - highlights the gameobject with the script
     
  7. shadowpuppet

    shadowpuppet

    Joined:
    May 7, 2015
    Posts:
    23
    nevermind, monkeyed with your scripts to find it. works although no clue as to why. found the gameobject. mesh renderer off so going to change that so I can find it again
    public class FindScript : MonoBehaviour {
    public MonoBehaviour sf;
    // Use this for initialization
    void Start () {

    sf= (MonoBehaviour) GameObject.FindObjectOfType (typeof(autoSpawn));

    Debug.Log(sf);
    }
    }