- Home /
How to check if a public method is referenced in inspector
Hi. I'm working on a project with tons of legacy C# code which isn't very developer friendly. Many methods in classes are public even if they aren't referenced outside of class. Some of those methods are connected to various objects in inspector (so they are called after you click a button or something) and I really don't know how to check that. Is there a way to check if a public method from some class is referenced somewhere in the inspector?
Sadly I don't know of such a way (outside of maybe going through every object and using tons of reflection to check if they use any methods).
Depending on size of the project I would suggest "scream check". Delete all methods that are not used in code and wait till you find something that doesn't work.
Won't most IDEs give you a "find uses" functionality for these kinds of things, or is there an extra step you're looking to automate?
@TreyH That works for regular method calls but unfortunately IDEs probably won't be able to figure out references through Unity's Event system (which is what's being used when you assign a method to a button in the inspector for example).
If you mean a program$$anonymous$$g IDE like VS, rider, etc, the use of a function in a UnityEvent or AnimationEvent won't be known to them.
Answer by Bonfire-Boy · Nov 19, 2019 at 12:40 PM
I'm not aware of a built in way to do this in the Unity editor. But the function (not the component it's called on) in a UnityEvent is simply serialised as the function name, so you can just search files for the name of the function, outside Unity.
I do this kind of stuff in bash using, for example...
To find all prefabs with a UnityEvent that invokes a function called FunctionName...
find . -name "*.prefab" -exec grep -l FunctionName {} \;
I would also search scene and scriptable object files which might contain UnityEvents - you may not have them in the latter. If necessary you can also search animation files this way (for functions being called from animation events). There are of course other ways of doing it depending on your platform etc
Note that if you wanted to know that a function's not being used anywhere, you'd also need to do a "find references" search in your IDE.
Answer by BastianUrbach · Nov 19, 2019 at 01:59 PM
If those references are just regular UnityEvents, then you can get the method name and target object with GetPersistentMethodName and GetPersistentTarget.
Answer by honor0102 · Apr 01 at 08:22 AM
You can use this for finding references to a method inside unity editor: https://github.com/recstazy/UnityEventReferenceViewer download the unitypackage file and use it according to help text