MissingComponentException: There is no component attached to the game object

ConsoleRuntime exception · Components

MissingComponentException: There is no 'X' attached to the "Y" game object, but a script is trying to access it.

A script tried to use a component, such as a Rigidbody or Animator, on a GameObject that does not have one. Unity names both the missing component and the GameObject.

Read the Console line first

X is the component type and Y is the GameObject name. Select that object and look for the component in the Inspector.

Common causes

  1. The component is on a different object

    The Animator is on the child model, not on the root where the script lives, or the other way round.

  2. 2D and 3D are mixed up

    The script asks for Rigidbody but the object has Rigidbody2D, or Collider when it has Collider2D. They are different components.

  3. Animation and Animator are mixed up

    Animation is the legacy component; Animator is the one that plays Animator Controllers.

The fix

Fix
C#
[RequireComponent(typeof(Rigidbody2D))]
public class Mover : MonoBehaviour
{
    private Rigidbody2D rb;

    void Awake() => rb = GetComponent<Rigidbody2D>();
}

Stop it coming back

[RequireComponent] makes Unity add the component when you attach the script and stops it from being removed. Every controller in this library that needs a Rigidbody declares it.

Questions

Why does a build show NullReferenceException for the same bug?
In the Editor, GetComponent returns a placeholder for a missing component that throws this exception and names the component and object. In a build it returns a real null, so you get a NullReferenceException instead. The fix is the same: add the component, or look for it on the right object.
How do I guarantee the component is always there?
Put [RequireComponent(typeof(Rigidbody))] above the class. Unity adds the component when the script is added and stops it being removed. It does not fix objects that already had the script before you added the attribute, so remove and re-add the script on those.
Should I use TryGetComponent instead?
Use TryGetComponent(out Rigidbody rb) when the component is optional, for example when a trigger may hit objects with or without health. It returns false instead of a placeholder, so you branch cleanly without an exception.

Scripts that avoid it

Related errors

Got a different error? Paste it into the error lookup.