godot: Rename a C# Script filename (NOT GdScript) referenced in a Scene, leaves the Scene with the old C# Script filename raising the error "can_instantiate: ..."

Godot version

4.2 RC 1 for .NET

System information

Windows 10

Issue description

Rename a C# Script filename (NOT a GdScript) referenced in a Scene, leaves the Scene with the old C# Script filename raising the error "can_instantiate: Cannot instance script because the associated class could not be found...".

IMPORTANT: This bug exists ONLY with C# code and NOT with GdScript.

Steps to reproduce

Simply create a new scene and attach a new C# script (NOT a GdScript), save all, after rename the attached C# script filename, save all and you will see that the Scene will continue to search for the old script filename. To fix you have to remove the script reference from the scene and add the new one.

Minimal reproduction project

Rename Project.zip

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 1
  • Comments: 15 (9 by maintainers)

Most upvoted comments

Rename a C# Script filename (NOT a GdScript) referenced in a Scene, leaves the Scene with the old C# Script filename raising the error "can_instantiate: Cannot instance script because the associated class could not be found...".

You’ve skipped the part of the error pointing at why it happens. Full error after renaming Main.cs to NewName.cs in the MRP:

can_instantiate: Cannot instance script because the associated class could not be found. Script: ‘res://NewName.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).

Renaming the script doesn’t change the contents of such script, meaning:

  • Before rename:
// Main.cs
using Godot;
using System;

public partial class Main : Node
{
}

Main class is found in Main.cs, all good.

  • After rename:
// NewName.cs
using Godot;
using System;

public partial class Main : Node
{
}

NewName class is not found in NewName.cs, hence the error.

Renaming Main class within NewName.cs to NewName makes the error go away:

// NewName.cs
using Godot;
using System;

public partial class NewName: Node
{
}

Not sure if that’s a bug.

Probably fixed by #85037

Edit: Unless you didn’t change the class name in the file like noted above