Quick answer: MonoBehaviours must live on a GameObject, so Unity forbids new MyBehaviour(). The object you get back is not attached to anything, compares equal to null, and never receives Start or Update. Use gameObject.AddComponent<T>() or instantiate a prefab; if the class does not need to be a component, remove : MonoBehaviour; for data assets use ScriptableObject.CreateInstance<T>().
This warning is easy to scroll past because the code compiles and does not throw on the line that caused it. The trouble comes later, as NullReferenceExceptions and logic that never runs, so it is worth fixing as soon as you see it.
- The exact warning
- Why the object behaves like null
- Fix 1: you need a component, so add it to a GameObject
- Fix 2: you do not need a component, so drop MonoBehaviour
- Fix 3: shared data, so use a ScriptableObject
- Hidden sources of the warning
- The NullReferenceExceptions it causes later
- Related problems
- Frequently asked questions
The exact warning
You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
Why the object behaves like null
A MonoBehaviour is a C# wrapper around a native component that the engine creates and attaches to a GameObject. new creates only the C# half. Unity overrides == on Unity objects to check the native half, so your freshly created object compares equal to null, gameObject and transform throw, and Unity never calls its Awake, Start or Update. Code that worked in your head silently does nothing.
Fix 1: you need a component, so add it to a GameObject
// Wrong
var health = new Health();
// Right: on an existing object
var health = gameObject.AddComponent<Health>();
// Right: on a new object
var go = new GameObject("Enemy");
var ai = go.AddComponent<EnemyAI>();
// Usually best: instantiate a configured prefab
EnemyAI enemy = Instantiate(enemyPrefab, spawnPoint.position, Quaternion.identity);
Pass setup values after AddComponent with an Init() method, because components cannot have constructors with parameters.
Fix 2: you do not need a component, so drop MonoBehaviour
Plenty of classes inherit from MonoBehaviour out of habit: inventory models, save data, state machines, damage calculators. If the class never needs to be on a GameObject, does not use Update and is not set in the Inspector as a component, make it a plain class. Then new is exactly right:
[System.Serializable]
public class InventorySlot
{
public string itemId;
public int count;
}
Marking it [System.Serializable] still lets it show inside another component's Inspector.
Fix 3: shared data, so use a ScriptableObject
For data that lives as an asset (weapon stats, level config), inherit from ScriptableObject. Create assets with a [CreateAssetMenu] attribute, or at runtime with ScriptableObject.CreateInstance<WeaponData>(). Using new on a ScriptableObject triggers a similar warning.
Hidden sources of the warning
- JSON deserialization:
JsonUtility.FromJson<T>and third-party serializers create instances withnew. For MonoBehaviours, useJsonUtility.FromJsonOverwrite(json, existingComponent), or deserialize into a plain data class and copy the fields. - Unit tests: create components with
new GameObject().AddComponent<T>()in tests, and destroy the GameObject in teardown. - Dependency injection containers configured to construct component types.
- Field initializers such as
private Health health = new Health();in another script.
The warning's stack trace points at the line. Double-click it in the Console.
The NullReferenceExceptions it causes later
The real cost of this warning is the NullReferenceException it produces somewhere else, sometimes only on a code path players reach. Bugnet's Unity SDK hooks Unity's log stream, so exceptions and errors in a player's build are filed automatically with the last log lines, a screenshot, the device, OS and build version, and identical ones stack into a single issue with a count.
Related problems
- How to Fix Unity's NullReferenceException
- How to Fix UnassignedReferenceException in Unity
- How to Fix “The associated script cannot be loaded” in Unity
Frequently asked questions
Why can’t I use new to create a MonoBehaviour in Unity?
MonoBehaviours wrap native components that must be attached to a GameObject. new only creates the C# object, which Unity treats as null and never updates.
How do I create a MonoBehaviour from code?
Call gameObject.AddComponent<T>() on an existing GameObject, add it to new GameObject(), or Instantiate a prefab that already has it.
How do I deserialize JSON into a MonoBehaviour?
Use JsonUtility.FromJsonOverwrite with an existing component instance, or deserialize into a plain serializable class and copy the values.
If it does not need Update or the Inspector, it does not need to be a MonoBehaviour.