Search Unity

How can I check whether a MonoBehaviour exists in a given source file with the same name of the file

Discussion in 'Scripting' started by jehovah0121qq, Apr 28, 2017.

  1. jehovah0121qq

    jehovah0121qq

    Joined:
    Nov 14, 2013
    Posts:
    68
    Using an editor script.
     
  2. laxbrookes

    laxbrookes

    Joined:
    Jan 9, 2015
    Posts:
    235
    Can I ask why you are trying to do this?
     
  3. jehovah0121qq

    jehovah0121qq

    Joined:
    Nov 14, 2013
    Posts:
    68
    I wanna check all scripts with a MonoBehaviour inside to see whether it's referenced by any asset. I wanna delete unused source code files from our project.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    If you get the script asset (using AssetDatabase), it's type will be MonoScript. MonoScript has a GetClass function which gives you the class in the script.

    I'm not sure what happens if the name doesn't match. Either GetClass still works, in which case you can compare names, or it doesn't, in which case GetClass probably returns null.
     
  5. jehovah0121qq

    jehovah0121qq

    Joined:
    Nov 14, 2013
    Posts:
    68
    Thanks very much.