Get Public Methods from MonoScript

Hi guys.

Is there a way to get all of the custom public Methods (functions), that are in a certain MonoScript? (NOT MonoBehaviour)

Lets say i have a EditorWindow and a field, where i can assign certain MonoScript…So i want to get a list of, only the Public Methods, that i have declared, not all possible methods…


Using reflection you can do that:

     MethodInfo[] methodInfos = obj.GetType().GetMethods();

obj is a reference to whatever type.

Ok that makes much more sense then, hmm…

Then you’re looking for GetClass() not GetType(), MonoScripts are just representations of the script assets not the actual classes. Change that and it should give you the public methods.

@instruct9r Hey!

I’m not sure why can’t you achieve what you want with just the type.GetMethods(flags) function, where flags = public | instance | declared only. That should give you the methods you need, if you want to get methods up the hierarchy, take out the declared only flag. Keep in mind public alone won’t work, you need ‘instance’ flag along with it. If you want to get very specific methods, you could just annotate those methods with an attribute you create, and then go type.GetMethods(flags).Where(m => m.IsDefined(typeof(MyAttribute)).ToArray();

If you want to invoke a method on an instance created from CreateInstance, you need to get a MethodInfo reference and then use your object on its Invoke function. i.e.

object theClass = Activator.CreateInstance(myType) ;
MethodInfo theFunction = myType.GetMethod("TheFunction", flags);
theFunction.Invoke(theClass, new object[] { arguments, if, any, ... });

In Unity 2019/2020 you can use this to show methods from gameobject:

                System.Reflection.MethodInfo[] method1 = this.GetType().GetMethods();
                foreach (var method in method1)