Unable to get variable from script

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Zerrtor

I have two scripts Bat and SwordhitBox. The bat script gives me this error:

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

I was following a tutorial, but I use C# and they don’t, and I’m not sure how to fix this even after searching though the Docs and google. Here’s the scripts below.

using Godot;
using System;

public class SwordHitbox : Area2D
{
	public Vector2 KnockbackVector = Vector2.Zero;
}

using Godot;
using System;

public class Bat : KinematicBody2D
{
	Vector2 knockback = Vector2.Zero;
	SwordHitbox swordHitbox = null;
	
	void _physics_process(float delta){
		knockback = MoveAndSlide(knockback);
	}
	
	private void OnHurtboxAreaEntered(object area){
		swordHitbox = (SwordHitbox)area;
	Knockback = swordHitbox.KnockbackVector * 60;

	}
}
:bust_in_silhouette: Reply From: juppi

Hi Zerrtor,

I assume OnHurtboxAreaEntered is a signal and you want to trigger it if the player/Bat is hit by a sword.

You should check if the Entering node is of type SwordHitbox.

private void OnHurtboxAreaEntered(Node body)
{
    if (body is SwordHitbox swordHitbox)
    {
        this.swordHitbox = swordHitbox;
        knockback = this.swordHitbox.KnockbackVector * 60;
    }
}