Search code examples
c#.netgodot4

Godot 4.2.2 mono. Incorrectly works a [Tool] tag in inspector


I have a code of Chunk and Block manager (like minecraft voxel world) and this code executed correctly in game without errors, but in inspector returned error

BlockManager.Instance NullReferenceException: Object reference not set to an instance of an object

I want to see result in inspector for editing and etc. I think Godot is not init a BlockManager in inspector.

/root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/ExceptionUtils.cs:112 - System.NullReferenceException: Object reference not set to an instance of an object.

     at Chunk.Generate() in C:\Users\kondy\OneDrive\Рабочий стол\RTS_MP\RTS\scripts\editor\Chunks\Chunk.cs:line 50
     at Chunk._Ready() in C:\Users\kondy\OneDrive\Рабочий стол\RTS_MP\RTS\scripts\editor\Chunks\Chunk.cs:line 36
     at Godot.Node.InvokeGodotClassMethod(godot_string_name& method, NativeVariantPtrArgs args, godot_variant& ret) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Generated/GodotObjects/Node.cs:line 2117
     at Godot.Node3D.InvokeGodotClassMethod(godot_string_name& method, NativeVariantPtrArgs args, godot_variant& ret) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Generated/GodotObjects/Node3D.cs:line 1035
     at Godot.CollisionObject3D.InvokeGodotClassMethod(godot_string_name& method, NativeVariantPtrArgs args, godot_variant& ret) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Generated/GodotObjects/CollisionObject3D.cs:line 564
     at Godot.PhysicsBody3D.InvokeGodotClassMethod(godot_string_name& method, NativeVariantPtrArgs args, godot_variant& ret) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Generated/GodotObjects/PhysicsBody3D.cs:line 210
     at Godot.StaticBody3D.InvokeGodotClassMethod(godot_string_name& method, NativeVariantPtrArgs args, godot_variant& ret) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Generated/GodotObjects/StaticBody3D.cs:line 137
     at Chunk.InvokeGodotClassMethod(godot_string_name& method, NativeVariantPtrArgs args, godot_variant& ret) in C:\Users\kondy\OneDrive\Рабочий стол\RTS_MP\RTS\Godot.SourceGenerators\Godot.SourceGenerators.ScriptMethodsGenerator\Chunk_ScriptMethods.generated.cs:line 88
     at Godot.Bridge.CSharpInstanceBridge.Call(IntPtr godotObjectGCHandle, godot_string_name* method, godot_variant** args, Int32 argCount, godot_variant_call_error* refCallError, godot_variant* ret) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/CSharpInstanceBridge.cs:line 24

github->Chunk.cs

github->BlockManager.cs

Error not becauce of cyrilic path to folder, I checked it. I write a code in Visual Studio 22, .NET 8.0.

Link to this repository on github: Repository in branch voxel-terrain.

I wrote a code from a tutorial in you tube and his code works. I want to see result in inspector for editing and ect.

I think Godot was edited in minor updates and my code needs adjustments.


Solution

  • In order to reference another script from within a Tool script, that other script must also have the [Tool] attribute.

    In this case, it seems that your BlockManager class does not have a [Tool] attribute, so when you try to get a reference to an instance of the BlockManager while in the editor, Godot does not have a valid reference to the BlockManager so it throws the error you are seeing.

    Adding a [Tool] attribute to the class declaration of BlockManager.cs should resolve the issue!