Creating your first script official guide not working for C#

Godot Version

4.2

Question

Hi, I’m following the official Creating your first script guide provided by Godot, trying to do it in C#, but it doesn’t work at the end returning the following error:

E 0:00:00:0730 can_instantiate: Cannot instance script because the associated class could not be found. Script: ‘res://sprite_2d.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:2417 @ can_instantiate()

I have set up visual studio code in the dotnet configs and installed the SDK for C#.

the script is correctly referenced in the Sprite2D node and it returns the error above.

The C# script is the following:

using Godot;

public partial class MySprite2D : Sprite2D
{
	private int _speed = 400;
	private float _angularSpeed = Mathf.Pi;

	public override void _Process(double delta)
	{
		Rotation += _angularSpeed * (float)delta;
		var velocity = Vector2.Up.Rotated(Rotation) * _speed;

		Position += velocity * (float)delta;
	}
}


Hmm, has the C# project been built? In the top right corner, there is a hammer button next to the play button in the Godot Editor; try clicking that and see if anything changes.

You can also try right clicking the script in your file panel and clicking re-import if that doesn’t work.

Other than that, I’m not sure as I use Visual Studio 2022.

It looks like the problem is the way the script is named in the File System. Change the name to: MySprite2D.cs

That should remove the error.

That worked! Thanks!

I’m gonna notify godot of the mistake in the page.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.