Skip to content

MonoBehaviors must be instantiated with GameObject.AddComponent instead of new

Matt Ellis edited this page Mar 26, 2018 · 3 revisions

If the new keyword is used to create a MonoBehaviour, the call will fail at run time. This is because a MonoBehaviour is a component, and needs to be attached to a GameObject. Without being attached, the special methods such as Start and Update, etc. won't get called.

This inspection will highlight any attempts to new a MonoBehaviour derived class, and provides an Alt+Enter quick fix to rewrite the new as a call to GameObject.AddComponent<T>(), allowing you to choose which GameObject to add the new component to.

This inspection corresponds to the Unity runtime error "You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed".

Clone this wiki locally