I have been experimenting with C# in Godot for the first time. All has been going well until I hit this issue. It should be an easy fix but I cant find a solution.

I'm just trying to print a simple message from the constructor when a 2nd class is instantiated.

Gist here

I get the error at build: "The type or namespace name 'OtherClass' could not be found (are you missing a using directive or an assembly reference?)"

Anybody put me out my misery?

Actually i'll post the code here as well. This script added to main node of scene:

using Godot;
using System;


public class Main : Node
{
    public override void _Ready()
    {
        OtherClass ot = new OtherClass();
    }
}

This script is saved in root folder of project, same as Main.cs:

using System;
using Godot;

public class OtherClass
{
    public OtherClass()
    {
        GD.Print("OtherClass created...");
    }
}

Thanks Megalomaniak, that is good to know.

I have found a workaround to the problem. Putting the OtherClass into the Main.cs file works:

using Godot;
using System;

public class Main : Node
{
    public override void _Ready()
    {
        GD.Print("Hello from C#");
        OtherClass ot = new OtherClass();
    }
}

public class OtherClass
{
    public OtherClass()
    {
        GD.Print("Hello from OtherClass");
    }
}

This is not ideal but it'll do for now until I can find how to reference a class from another file.

8 days later

Try to inherit your OtherClass from a node : public class OtherClass : Node

I did try that, unfortunately it doesn't work for me either. Still stuck with this one I'm afraid.

Did you checked the .cspoj file? Maybe is a silly question :)

It's not a silly question because I not entirely sure what I am doing lol.

I had checked .csproj but could not find a reference to 'OtherClass'. As i am still getting familiar with C# in Godot, I wasn't sure if it should be referenced there as the namespace may sort that out. I am used to using C# in visual studio and having VS look after everything for me.

Do I have to manually enter any referenced classes in the .proj file?

Yes. The compiler must know all classes and where they are in order to be able to compile them. the .csprj is the setup for the compiler :)

In my project I have something like this:

 <ItemGroup>
    <Compile Include="addons\EditorSettings\plugin.cs" />
    <Compile Include="assets\resources\Bot.cs" />
.... other classes ....
</ItemGroup>

I guess you have to add your class there too. :)

You should always create your C# scripts from the godot editor to prevent this from happening. The csproj file gets automatically updated if you do it that way. I have seen a request of using a more updated csproj format on the godot github which doesn't require you to have every file listed in the csproj, so who knows, might become part of future godot!

@robbertzzz said: You should always create your C# scripts from the godot editor to prevent this from happening. The csproj file gets automatically updated if you do it that way.

It work if you add a new file, But if you try to move it or to delete it godot doesn't update the csprj file automatically. For now - until it will be fixed - you have to check it every time you will get that specific error. :)

Yes. That got it! Thank you @Vale-git, it's a relief to get this solved.

That's good info to know too @robbertzzz , I just naturally added a new class in the IDE i was using without giving it thought. It's good to learn a bit more about the .csproj file and understand how it's all glued together. I guess that's the downside to being babysat by your IDE.

Thanks for your help guys!

3 years later