Unity Tip: Use TryGetComponent instead of GetComponent to avoid memory allocation in the Editor

If you are a Unity developer, you may find that memory allocation in the editor when you call GetComponent() to query for a component that doesn’t exist. Just like the screenshot below.

It should be noted that this memory allocation will only occur in the Editor. But why is this memory allocation…

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jiadong Chen

Jiadong Chen

Senior Software Developer at Company-X | Microsoft MVP, MCT | Member of .NET Foundation | Azure Certified Solutions Architect Expert