Quick answer: MissingComponentException means GetComponent found nothing on that GameObject and the script used the result anyway. Add the component the message names, or fix the lookup: the component is on a child or parent, is the 2D or 3D twin of the type you asked for, or was removed. Use [RequireComponent] for mandatory components and TryGetComponent for optional ones. In a player build the same mistake is a NullReferenceException.

This is one of the more helpful messages Unity prints: it names the component, names the GameObject and suggests what to do. It still sends people in circles, because the component is often visible in the Inspector, only on a child object, on the parent, or as the 2D version of the type the script asked for. Below: what the message says and when it fires, why you only ever see it in the editor, the causes in order of how often they turn up, a table of component types that get confused, and four fixes with code for Unity 6 and 2022 LTS.

The exact error

MissingComponentException: There is no 'Rigidbody' attached to the "Player" game object, but a script is trying to access it.
You probably need to add a Rigidbody to the game object "Player". Or your script needs to check if the component is attached before using it.
UnityEngine.Rigidbody.AddForce (UnityEngine.Vector3 force) (at <hash>:0)
PlayerMotor.FixedUpdate () (at Assets/Scripts/PlayerMotor.cs:9)

Three facts are in there: the component type that was requested (Rigidbody), the name of the GameObject that was searched (Player), and, in the stack trace, the script line that used the result. The code behind it looks like this:

public class PlayerMotor : MonoBehaviour
{
    void FixedUpdate()
    {
        GetComponent<Rigidbody>().AddForce(Vector3.forward * 10f);   // line 9
    }
}

GetComponent itself never throws. It returns nothing, and the exception fires at the first member you touch on that result, here AddForce. When the lookup is cached in Awake and used in FixedUpdate, the trace points at the use, and the failed lookup is the line you need to find.

Why you only see it in the editor

In the editor, a failed GetComponent for a built-in component type does not return a real C# null. It returns a placeholder object that compares equal to null and carries the text of this message, so that touching it produces a named exception with the object and component in it. That placeholder is also why Unity’s documentation notes that TryGetComponent “does not allocate in the Editor when the requested component does not exist”, while GetComponent does.

A player build skips the placeholder. GetComponent returns a real null, and the same line throws:

NullReferenceException: Object reference not set to an instance of an object

So a MissingComponentException you ignore in the editor ships as a NullReferenceException with no component or object name attached. Two consequences. First, if (rb == null) works in both places, because Unity’s == treats the placeholder as null, but rb is null and rb?.AddForce(...) do not see it in the editor. Second, the named exception shows up for Unity’s native component types such as Rigidbody, Collider, Animator, AudioSource and Renderer. When the missing component is a script (your own, or a package component such as a TextMeshPro text), you normally get a plain NullReferenceException at the same line in the editor too. The causes and fixes below are identical for both.

What causes it

  1. The component was never added to this object. The script was dropped on a new GameObject or a new prefab and nobody added the Rigidbody it assumes. Also check for a second copy of the script on an object that was never meant to have it, such as the camera or an empty parent: the message names the object, so read the name.
  2. The component is on a child or a parent. The usual layouts: Rigidbody on the root with the script on a child, or Animator and renderers on an imported model child with the script on the root. GetComponent searches one GameObject only.
  3. 2D and 3D types are mixed up. The object has a Rigidbody2D and the script asks for Rigidbody, or the reverse. They are unrelated classes.
  4. The lookup runs on the wrong object. In OnTriggerEnter(Collider other), other is the collider that was touched, often a child of the object that owns the Rigidbody. GameObject.Find("Player") returns the first active object with that name, which may not be the one you meant.
  5. A prefab variant or override removed it. The base prefab has the component, and a variant or one scene instance carries a “removed component” override. A different prefab that reuses the script fails the same way.
  6. It was removed at runtime. Something called Destroy(GetComponent<Collider>()) and a later GetComponent finds nothing. If you cached the component before it was destroyed, the error is a MissingReferenceException instead.

Component types that get confused

The script asks forThe object hasWhat to do
RigidbodyRigidbody2DUse the 2D type throughout; 2D and 3D physics do not interact
Collider, BoxColliderCollider2D, BoxCollider2DGetComponent<Collider>() never returns a 2D collider
MeshRendererSkinnedMeshRenderer or SpriteRendererAsk for the base class Renderer if any renderer will do
AnimatorAnimation (legacy)Check the import settings; legacy clips use the Animation component
RigidbodyCharacterControllerA CharacterController is moved with Move, it has no Rigidbody
Text (uGUI)TextMeshProUGUIReference TMP_Text, the base of both TextMeshPro types
TextMeshProTextMeshProUGUIThe first is the world-space text, the second is the UI text
RectTransformA plain TransformOnly UI objects have a RectTransform

How to find the object and the lookup

  1. Double-click the Console entry. It opens the first line of your own code in the trace, which is the line that used the missing component.
  2. Find the GetComponent call that fed that variable. If it is on the same line, you have it. If the variable is a field, search the script for where it is assigned.
  3. Find every object carrying the script. Type t:PlayerMotor in the Hierarchy search box to list all instances in the open scenes, and select each one.
  4. When several objects share a name, make the failing one clickable. Add a temporary log with a context object, then single-click the entry in the Console to highlight that exact object:
    void Awake()
    {
        if (!TryGetComponent<Rigidbody>(out _))
            Debug.LogError($"{name} has PlayerMotor but no Rigidbody", this);
    }
  5. On a prefab instance, open the Overrides dropdown in the Inspector header and look for a removed component.

Fix 1: make the dependency explicit with RequireComponent

[RequireComponent(typeof(Rigidbody))]
public class PlayerMotor : MonoBehaviour
{
    Rigidbody body;

    void Awake()
    {
        body = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        body.AddForce(Vector3.forward * 10f);
    }
}

With the attribute in place, adding PlayerMotor to a GameObject adds a Rigidbody automatically, and the editor refuses to remove the Rigidbody while the script is still there. One limit matters: the check only runs when the script is added, in the editor or through AddComponent. Objects and prefabs that already had the script before you wrote the attribute are not repaired. Add the component to those by hand.

Caching in Awake is part of the fix. It moves the lookup to one place, so there is a single line to check when the component is missing.

Fix 2: look on the object that has the component

// Script on a child, Rigidbody on the root
body = GetComponentInParent<Rigidbody>();

// Script on the root, Animator on the imported model child
animator = GetComponentInChildren<Animator>();

// The child starts inactive
animator = GetComponentInChildren<Animator>(true);

Both methods check the GameObject itself first, then walk up or down the hierarchy. By default they skip inactive objects; pass true for includeInactive to search those as well.

In physics callbacks, use what the callback already gives you. A collider knows which Rigidbody it belongs to, even when that Rigidbody is on a parent:

void OnTriggerEnter(Collider other)
{
    Rigidbody hitBody = other.attachedRigidbody;   // null when there is none
    if (hitBody != null)
        hitBody.AddForce(Vector3.up * 5f, ForceMode.Impulse);
}

Fix 3: TryGetComponent when the component is optional

If it is legitimate for the object not to have the component (not everything a bullet hits has a Rigidbody), say so in the code:

void OnCollisionEnter(Collision collision)
{
    if (collision.gameObject.TryGetComponent(out Rigidbody hitBody))
    {
        hitBody.AddForce(-collision.relativeVelocity, ForceMode.Impulse);
    }
}

TryGetComponent returns false and leaves the out variable null when nothing is found, in the editor and in builds alike. Reserve it for components that are truly optional. Wrapping a mandatory lookup in it only hides the setup mistake: the error goes away and so does the movement.

Fix 4: assign the reference in the Inspector and validate it

A serialized field removes the runtime lookup and lets the component live on any object:

[SerializeField] Rigidbody body;

void Reset()
{
    // Runs when the script is first added, and from the Reset menu command
    body = GetComponentInParent<Rigidbody>();
}

void OnValidate()
{
    // Editor only: runs when the script loads or a value changes in the Inspector
    if (body == null)
        Debug.LogWarning($"{name}: PlayerMotor has no Rigidbody assigned", this);
}

If the slot is left empty, the editor error changes to an UnassignedReferenceException, which names the field. The OnValidate warning catches it before you press Play.

How to confirm the fix

Enter play mode in every scene that uses the object, and exercise the code path, since a lookup inside a collision handler only runs when that collision happens. For prefabs spread across the project, an edit mode test catches the next person who breaks the setup:

[Test]
public void EveryPlayerMotorHasARigidbody()
{
    foreach (string guid in AssetDatabase.FindAssets("t:Prefab"))
    {
        string path = AssetDatabase.GUIDToAssetPath(guid);
        var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(path);
        foreach (var motor in prefab.GetComponentsInChildren<PlayerMotor>(true))
            Assert.IsTrue(motor.TryGetComponent<Rigidbody>(out _), path);
    }
}

The test uses TryGetComponent on purpose. Assert.IsNotNull(motor.GetComponent<Rigidbody>()) would pass on a broken prefab, because NUnit does a plain reference check and the editor’s placeholder object is not a real null.

Finally, run a development build through the same path and search Player.log for NullReferenceException. That is the form this bug takes outside the editor.

What reaches you from a shipped build

Players never see the helpful version of this message. Their build throws a NullReferenceException with no component name and no object name. The Bugnet Unity SDK files that exception automatically with its stack trace, recent log lines, platform, OS version and game version, which gives you the script and method, and the line if your build keeps line numbers. To get the names back, log them yourself: the Debug.LogError call from the diagnosis step, left in Awake, is also captured, because the SDK files logs of type Error as well as exceptions. One report then reads “Crate_03 has PlayerMotor but no Rigidbody” where otherwise you would have an anonymous null reference. Bugnet cannot tell you which prefab variant lost the component; the name in your own log line does that. Setup is in the getting started guide.

Frequently asked questions

What does MissingComponentException mean in Unity?

A script asked a GameObject for a component with GetComponent, the GameObject does not have that component, and the script used the empty result anyway. The message names the component type and the GameObject so you can add the component or correct the lookup.

Why do I get MissingComponentException when the Rigidbody is attached?

The component is usually attached somewhere else or is a different type. Check whether it sits on a child or parent object, whether it is a Rigidbody2D while the script asks for Rigidbody, and whether a second object with the same script lacks it. The object name in the message tells you which one failed.

Why does my build throw NullReferenceException where the editor showed MissingComponentException?

The named exception is an editor feature. In the editor a failed GetComponent returns a placeholder that carries the error text. A player build returns a real null, so the same line throws a plain NullReferenceException without the component or object name.

Does RequireComponent add the component to objects that already have the script?

No. RequireComponent is checked only when the script is added to a GameObject, in the editor or through AddComponent at runtime. Existing objects and prefabs are left as they are, so add the component to them manually or reset the script component.

Should I use GetComponent or TryGetComponent?

Use GetComponent, cached in Awake and backed by RequireComponent, when the component must exist. Use TryGetComponent when the component is optional, such as checking whether the thing you hit has a Rigidbody. TryGetComponent also avoids an editor-only allocation when nothing is found.

The message names the GameObject that was searched. Read that name before anything else: most of the time the component exists, on a different object or as a different type.