error CS0120: An object reference is required for the non-static field, method, or property

ConsoleCompiler error · Compiler

error CS0120: An object reference is required for the non-static field, method, or property 'X'

You called something through the class name, PlayerHealth.TakeDamage(10), but TakeDamage belongs to each PlayerHealth component, not to the class. You need a reference to the specific component.

Read the Console line first

X names the member you called through the class name.

Common causes

  1. Calling a component method by its class name

    Each PlayerHealth on each GameObject has its own health. The compiler cannot know which one you mean.

  2. Using an instance field from a static method

    A static method has no this, so it cannot read the object's fields.

The fix

Fix
C#
void OnCollisionEnter(Collision collision)
{
    // Get the component on the object you hit
    if (collision.gameObject.TryGetComponent(out PlayerHealth health))
        health.TakeDamage(10);
}

Stop it coming back

Reach other objects through a reference: a serialized field, GetComponent, a collision or trigger, or a singleton's Instance when there is only ever one.

Questions

Why can't I call PlayerHealth.TakeDamage() directly?
TakeDamage belongs to one particular PlayerHealth component, and the compiler needs to know which one. Get it first, for example other.GetComponent<PlayerHealth>().TakeDamage(10);, or keep a serialized reference to it.
Should I just make the method static?
Only when there is truly one of something for the whole game. Static fields do not appear in the Inspector, survive scene loads, and can keep stale values between Play sessions when domain reload is turned off. For a single global manager, a singleton gives you one instance with a static accessor.
What does 'object reference' mean here?
A reference to a specific instance of the class: a component on a GameObject, or an object you created with new for plain C# classes. Static members belong to the class itself and need no instance.

Scripts that avoid it

Related errors

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