error CS1061: 'X' does not contain a definition for 'Y'

ConsoleCompiler error · Scripting

error CS1061: 'X' does not contain a definition for 'Y' and no accessible extension method 'Y' accepting a first argument of type 'X' could be found (are you missing a using directive or an assembly reference?)

The compiler looked for a member called Y on type X and found none. In Unity this usually means the variable holds a different type than you think, such as a GameObject instead of a component, or the name is spelled differently from the API.

Read the Console line first

The message names both types: X is the type of the variable before the dot, Y is the member you asked for. The file and line are at the start of the line, for example Assets/Scripts/Player.cs(18,12).

Common causes

  1. The variable is a GameObject, not the component

    GameObject has no velocity, health or TakeDamage. Those live on components. Get the component first.

    Example
    C#
    public GameObject enemy;
    
    void Hit()
    {
        enemy.TakeDamage(10); // CS1061: GameObject has no TakeDamage
    }
  2. The name is spelled or capitalised differently

    C# is case-sensitive. transform.Position, rigidbody.Velocity and gameObject.SetActive(True) all fail. Unity APIs use PascalCase for methods and camelCase for properties such as position and velocity.

  3. An extension method's namespace is missing

    LINQ methods such as Where, Select and First are extension methods in System.Linq. Without using System.Linq; at the top, the compiler reports CS1061 for them.

  4. The member was removed or renamed in a newer Unity

    Code written for an older version can call APIs Unity has since removed. Check the Unity 6 upgrade pages, or paste the script into the script checker.

The fix

Fix
C#
public GameObject enemy;

void Hit()
{
    if (enemy.TryGetComponent(out Health health))
        health.TakeDamage(10);
}

// Or keep a reference of the component type in the first place:
// [SerializeField] private Health enemyHealth;

Stop it coming back

Type serialized fields as the component you need (Health, Rigidbody) rather than GameObject, so the Inspector only accepts objects that have it and the compiler knows its members. Let your IDE autocomplete Unity APIs instead of typing them from memory.

Questions

Why does GetComponent not appear when I type a dot after my variable?
If the variable is a component, GetComponent is there; if autocomplete shows nothing at all, your IDE is not connected to Unity's project files. Set your editor in Preferences > External Tools and choose Regenerate project files.
The code compiled in an older Unity version. Why not now?
Unity removes APIs some versions after marking them obsolete. Paste the script into the Unity 6 script checker to see which calls changed and what replaces them.
How is CS1061 different from CS0103?
CS1061 is about a member after a dot: the type exists, but it has no member with that name. CS0103 is about a name on its own that the compiler cannot find at all.

Upgrading to Unity 6?

This error can also come from an API Unity 6 changed. ModelImporter APIs removed in Unity 6.5: isFileScaleUsed, optimizeMesh and more.

Scripts that avoid it

Related errors

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