Search Unity

Find all the scripts existent in my scene

Discussion in 'Scripting' started by Deleted User, Oct 31, 2016.

  1. Deleted User

    Deleted User

    Guest

    Hiya! I need to find every script which I've attached to any object in my scene. I'm curious to know how I would accomplish this relatively efficiently.

    I found this: http://answers.unity3d.com/question...ene.html?_ga=1.33654369.1349432708.1474332100

    But its only concerned with finding a particular script. I want to find all of them.

    I want to do this so that I may create a dynamic system capable of finding all scripts in my scene, so that those which have been placed on an object can "know" what other scripts are available to them.

    This is all in effort to create "loose coupling". I'm attempting to do this because I can get stuff to build but if it all the scripts aren't instantiated then I run in to problems (specifically null refs).

    This system should let me just place scripts on objects without having to worry about null refs or writing repetitive code to make sure things don't access things which aren't there!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    For the type, use MonoBehaviour.

    --Eric
     
    Bunny83 likes this.
  3. Deleted User

    Deleted User

    Guest

    I don't understand. Use Monobehavior how?? I just checked the API docs. I don't see anything that would let me find all the scripts currently in the scene. I could try a for...next loop which runs through all possible scripts in my game, in conjunction with getObjectsByType() but I'm not sure how doable that would be. Also I would have to add new types by code every time I implemented a new class...
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The answer you linked to above, do that, but use MonoBehaviour for the type. All your scripts derive from MonoBehaviour (or else you wouldn't be able to attach them to objects).

    --Eric
     
    rahulk1991 likes this.
  5. Deleted User

    Deleted User

    Guest

    Well damn if I didn't think of that! Thanks man!
     
  6. kerem-yokuva

    kerem-yokuva

    Joined:
    Nov 21, 2015
    Posts:
    10
    I know it is an old post. But my search directed me to here.
    So here is my 2 cents

    Code (CSharp):
    1. [MenuItem("Tools/Find Scripts")]
    2.     private static void MTwo()
    3.     {
    4.         MonoBehaviour[] armyUnits = Object.FindObjectsOfType<MonoBehaviour>();
    5.         foreach (MonoBehaviour v in armyUnits)
    6.         {
    7.             Debug.Log(v.name+"-"+v.gameObject.name);
    8.         }
    9.     }
     
    SazzadKhan likes this.
  7. crawfis

    crawfis

    Joined:
    Jan 30, 2014
    Posts:
    114
    Slight change to @kerem-yokuva 's solution. MonoBehaviour.name == gameObject.name, so you need to get the Type and then the Name of the Type.
    Code (CSharp):
    1.                 Debug.Log(script.GetType().Name + " is used within " + script.gameObject.name);
    2.  
    Note: I replaced "v" with "script" for the variable name. Full script:
    Code (CSharp):
    1.     internal class FindScriptsInScene
    2.     {
    3.         [MenuItem("Tools/Find Scripts")]
    4.         public static void FindScripts()
    5.         {
    6.             MonoBehaviour[] scripts = UnityEngine.Object.FindObjectsOfType<MonoBehaviour>();
    7.             foreach (MonoBehaviour script in scripts)
    8.             {
    9.                 Debug.Log(script.GetType().Name + " is used within " + script.gameObject.name);
    10.             }
    11.         }
    12.     }
    13.  
     
    Last edited: Dec 6, 2022
    AnimalMan likes this.
  8. crawfis

    crawfis

    Joined:
    Jan 30, 2014
    Posts:
    114
    FYI. This works with all loaded scenes in the Editor (for those using Additive scenes). Also, played with the script some to remove scripts from Unity (Point Lights , Text, etc.). Enjoy.
    Code (CSharp):
    1.     internal class FindScriptsInScene
    2.     {
    3.         [MenuItem("Tools/Find Scripts")]
    4.         public static void FindScripts()
    5.         {
    6.             MonoBehaviour[] scripts = UnityEngine.Object.FindObjectsOfType<MonoBehaviour>();
    7.             foreach (MonoBehaviour script in scripts)
    8.             {
    9.                 Type scriptType = script.GetType();
    10.                 var scope = scriptType.Namespace;
    11.                 if(scope == null || !scope.StartsWith("Unity"))
    12.                     Debug.Log(script.GetType().FullName + " is used within " + script.gameObject.name);
    13.             }
    14.         }
    15.     }
    16.  
     
    AnimalMan likes this.