Issue with C# Script Instantiation in Godot 4.2.2

Hi everyone,

I’m currently working with Godot version 4.2.2.stable.mono.official on Windows 10, and I’ve encountered an issue with my C# script. The error message I’m receiving is as follows:

E 0:00:01:0580   can_instantiate: Cannot instance script because the associated class could not be found. Script: 'res://Assets/Scenes/player.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 understand that this error typically occurs if the filename does not match the class name within the script, considering the case sensitivity. However, in my case, the player.cs script name matches the class name declared in the script exactly.

I’ve tried starting a fresh new project, but the issue remains unresolved and made sure the script is located at res://Assets/Scenes/player.cs. I’ve also made sure to install the latest Dotnet incase it was something related to that. Despite my efforts, the error persists.

Since this is my first project with Godot, I’m stuck on this issue and haven’t been able to find a solution online. If anyone has any insights or suggestions to resolve this, I would greatly appreciate your help!

Thank you in advance!

1 Like

What is calling this script? I assume player.tscn. Could you show that as well?

1 Like

try open in Visual Studio Code or any c# coding tool, Godot is not enough.
classes from small letter should not affect but you can do right from beginning :slight_smile:.

using Godot;
using System.Collections.Generic;
public partial class MyPlayerClass:CharacterBody2D {
	[Export]
	public string m_some_string = "Hello, world";
	[Export]
	public int m_some_integer = 99;
}
public partial class MyCsharp:Node2D {
	public override void _Ready() {
		MyPlayerClass v_my_player_class = new MyPlayerClass();
		AddChild(v_my_player_class);
	}
}

it looks like you want to instantiate player and player inherits from CharacterBody2D . would new player() work?

Nope. It doesn’t really have to do anything with the script. Since the error is coming from the name of the class not matching the script name despite the names matching. I don’t think changing the script in anyway will solve the issue.

I’ve tried this already but it just opens a empty visual studio script. Opening it straight from the file just opens it through a miscellaneous file. This also doesn’t change the situation.

image

It’s just a very simple player scene. For reference I was following brackeys tutorial on Godot I got up to about 15 minutes.

Just an update on the issue. I believe it’s because it fails to create a C# project so it’ll make a csharp file without a solution. I think this maybe the issue. This is what happens when I try to build a C# project > tools > c# > create c# project.

It also comes up with an error saying that .NET SDK hasn’t been found but I do have the correct version installed (8.0.6) and I’ve repaired it twice.

image