Can_instantiate error on C# example from tutorial

Godot Version

Godot_v4.2.1-stable_mono_linux.x86_64

Question

I am getting a can_instantiate error when following the online step-by-step tutorial for creating and attaching a basic C# script.

I am at this step of the step-by-step tutorial: Creating your first script — Godot Engine (stable) documentation in English

These are the contents of my script, named MySprite2D.cs:

using Godot;
using System;

public partial class MySprite2D : Sprite2D
{
	Print("Hello, world!");
}

Running the project with F6 does not print “Hello, world!” to output like the tutorial suggests it should. This is the error I get in the debugger:

E 0:00:00:0700 can_instantiate: Cannot instance script because the associated class could not be found. Script: ‘res://MySprite2D.cs’. Make sure the script exists and contains a class definition with a name that matches the filename of the script exactly (it’s case-sensitive).
<C++ Error> Method/function failed. Returning: false
<C++ Source> modules/mono/csharp_script.cpp:2423 @ can_instantiate()

Here is a screenshot of my setup in the Godot editor in case it’s helpful.

I am able to get the example working if I use GDScript, so I think it’s either something to do with how I am writing the C# script or a bug.

Hello :slightly_smiling_face:
Your script is not compiling because you wrote what’s probably supposed to be in a method directly in the class. Following the page you linked, it should look more like the following:

using Godot;

public partial class MySprite2D : Sprite2D
{
    public MySprite2D()
    {
        // This is a constructor, it will run whenever an instance
        // of MySprite2D is created.
        GD.Print("Hello, world!");
    }
}

Oh god i’m a buffoon, you’re right :person_facepalming: thanks for the help!

Aaah, it happens. That error message doesn’t really help by being quite vague, too.