• Godot Help
  • [C#, 4.2] Area2D does not contain a definition for...

Hi Everyone!

I'm calling a public method, Die, on an Area2D, from another class:

public void OnAreaEntered(Area2D area)
{
area.Die();
}

However, I'm getting this error message:

"CS1061: 'Area2D' does not contain a definition for 'Die' and no accessible extension method 'Die' accepting a first argument of type 'Area2D' could be found"

Area2D does not itself have this method, of course, but the class, Enemy, that inherits from it, does. But, I've tried this, and get the same error message:

public void OnAreaEntered(Enemy area)
{...

I've discovered that setting the parameter type to dynamic makes the error message go away and the script compiles:

public void OnAreaEntered(dynamic area)
{...

But, this doesn't seem like a proper solution.

Does anyone happen to know what might be going on here or what the best solution is?

Thank you!

D13

  • spaceyjase replied to this.
  • spaceyjase

    That almost worked, thank you!
    I just needed to change the as to is

    if (area is Enemy enemy)
    {
        enemy.Die();
    }

    d13 Cast the area to the correct type, e.g.

    if (area as Enemy enemy)
    {
        enemy.Die();
    }
    • d13 replied to this.

      spaceyjase

      That almost worked, thank you!
      I just needed to change the as to is

      if (area is Enemy enemy)
      {
          enemy.Die();
      }

      Hey, I changed the tag to the more appropriate one. Also, selected the best answer, as it seems you've solved it 🙂