Confusion with types in C#

I’ve got a riddle here,

This code is within a class of type CharacterBody3D,

if (GetSlideCollisionCount() > 0)
    KinematicCollision3D hitInfo = GetLastSlideCollision();
    GodotObject hitCollider = hitInfo.GetCollider();

    string physMatPath = "";
    if (hitCollider is StaticBody3D staticBody)
        physMatPath = staticBody.PhysicsMaterialOverride.ResourcePath;
    else if (hitCollider is RigidBody3D rigidBody)
        physMatPath = rigidBody.PhysicsMaterialOverride.ResourcePath;
    else if (hitCollider is Node3D node)
        GD.Print("This will be reached, but the type printed here is StaticBody3D!", hitCollider);

        if (node is StaticBody3D staticBody2)
            GD.Print("This will not be reached");

The question is: How do we cast hitCollider to StaticBody3D so that we may access the physics material property?

We’d normally just write, if (hitCollider is StaticBody3D staticBody) { ... }

However, I seem to have caused a problem by having attached a script to this StaticBody3D node in the scene. The script is of type Node3D, which makes if (hitCollider is StaticBody3D) return false.

How do we get at the node’s true type so that we may cast it to a StaticBody3D?

Does this even sound right?

I imagine you mean that you attached a script that is extending Node3D to that node. If so, that is not something you should/can do. If you have a StaticBody3D node, then the script attached should extend StaticBody3D, not Node3D.

1 Like

I see, if that’s how it works, then that’s how it works!