Skip to content

Avoid using string based Method Invocation

Matt Ellis edited this page Feb 15, 2019 · 3 revisions

Certain Unity APIs take a string value that is the name of a method to invoke. This can be very inefficient, and it is recommended to avoid these methods inside a performance critical context. For example, MonoBehaviour.Invoke works very much like reflection, having to look up the method by name on the current behaviour. The SendMessage functions are even more expensive, looking for the named method on each MonoBehaviour attached to the game object.

Each usage inside a performance critical context will be highlighted. Context actions that move the invocation to Start or Awake are available in Alt+Enter, although be careful, as this does change the semantics of your code - the named method is invoked at a different time. Other ways to improve performance may require more general rewriting to avoid these methods.

The methods highlighted include:

This inspection was first added in Rider/ReSharper 2018.3

Clone this wiki locally