"USER ERROR: Scene instance is missing" when export with release in Godot_v4.3-stable_mono

Godot Version

Godot_v4.3-stable_mono_win64

Question

After exporting my game using Export with Release, there is about a 20% chance that the following error will occur when opening a specific page:

USER ERROR: Cannot get class ''.
   at: _instantiate_internal (core/object/class_db.cpp:515)
USER WARNING: Node  of type  cannot be created. A placeholder will be created instead.
   at: instantiate (scene/resources/packed_scene.cpp:277)
USER ERROR: Cannot get class ''.
   at: _instantiate_internal (core/object/class_db.cpp:515)
USER WARNING: Node  of type  cannot be created. A placeholder will be created instead.
   at: instantiate (scene/resources/packed_scene.cpp:277)
USER ERROR: Cannot get class ''.
   at: _instantiate_internal (core/object/class_db.cpp:515)
USER WARNING: Node  of type  cannot be created. A placeholder will be created instead.
   at: instantiate (scene/resources/packed_scene.cpp:277)
USER ERROR: Scene instance is missing.
   at: instantiate (scene/resources/packed_scene.cpp:244)

I am using C# for development, and the error seems to be coming from the following code:

var packagedScene = ResourceLoader.Load<PackedScene>(path); 
var instance = packagedScene.Instantiate();

The issue is that during the development phase and when using Export With Debug, this problem never occurs, even after extensive testing. However, it only happens when using Export with Release.

I’m reaching out to see if anyone else has encountered this issue and found a solution, or if there are any specific things to watch out for when using Export with Release.

你把path打印一下

I had the same problem with you. I tried create this piece of code.
Replace all the default ResourceLoader.Load() with my version and the problem went away.

public static class Utils {

    public static Dictionary<string, PackedScene> CachedScenes = new();

    public static T CreateInstanceFromScene<T>(string path) where T : Node {
        if (CachedScenes.ContainsKey(path) == false) {
            CachedScenes[path] = ResourceLoader.Load<PackedScene>(path, null, ResourceLoader.CacheMode.IgnoreDeep);
        }
        return CachedScenes[path].Instantiate<T>();
    }
}