0 votes

I just create a new project. I want to get a missile to fire straight.

I started with a .cs file and got my missile to fire straight. I then decided I wanted to try it with a .gd script. I deleted my .cs file copy and pasted some code from the godot 2D tutorial.

I went to start the game and my game wouldn't compile because it couldn't find the .cs file. So just to mess around quickly I created the Missile.cs that was missing. I did this by right clicking on my res:// folder and created a new .cs file. When it first showed up it was named new_script.cs. I changed that name to Missile.cs and hit run.

Now it won't compile because it can't find new_script.cs. So I create another new file and left the name new_script.cs.

Now my error is: The namespace '' already contains a definition for 'new_script'

What have I done wrong here?

Did I delete the files incorrectly?
It seems weird that I can't just delete files that aren't being used by any of my objects.

I want to be able to delete files that I'm not using without such a big fuss.

asked Jul 22, 2019 in Engine by funlamb (70 points)

1 Answer

0 votes
Best answer

First of all, when you use C#, compiled scripts are not referenced the same way as GDScript. C# uses a project file to list what goes into the assembly (this is general to C#, it's not just a Godot thing, look it up). I think Godot creates it if it does not exist, but it doesn't re-generates it everytime (otherwise it would loose any compilation settings you put in it). This project file is used by Mono to compile, and is very often used by IDEs and code editors like Visual Studio, Mono Develop, Rider or VSCode. Editing from Godot works but it's barely raw text edition.

When it first showed up it was named new_script.cs. I changed that name to Missile.cs and hit run.

This is not how C# works. Changing the name of the file does not change the class name inside it. That's two different things. You probably ended up with a file named Missile.cs but a class inside named new_class.

Now my error is: The namespace '' already contains a definition for 'new_script'

Because of the above, you now have two different files, but they both contain a class named new_script, which causes a conflict. It's a good practice in C# to make sure the class name in a file matches the name of the file. I think Godot also relies on this to find which class to instanciate (since you can have more than one class in a file).

Now it won't compile because it can't find new_script.cs

Check also your C# project (it's a .csproj file). You can do this by opening it with one of the editors I mentionned above. You can also check with a plain text editor (it's XML inside), and make sure it references your script. If not... add it, either from an IDE or by editing the XML. Again, C# is a bigger beast than GDScript and is best edited with an actual IDE.
If you don't want that complexity, just use GDScript.

It seems weird that I can't just delete files that aren't being used by any of my objects. I want to be able to delete files that I'm not using without such a big fuss.

Just delete the files then. You didn't delete any so far, only renamed them.

answered Jul 23, 2019 by Zylann (26,149 points)
selected Jul 25, 2019 by funlamb

Thanks for such a thorough response.

I have deleted the files. Both Missiles.cs and new_script.cs are not in my res:// folder or in any underlying folders.

I checked the .csproj file like you suggested. I did that using Visual Studio 2017. It used that IDE as the default program. I found in the soulution explorer that the Missile.cs and new_script.cs files where missing. I couldn't find any referance in any other file probably because I don't know where I should be looking. Anyway I deleted those two missing files from the solution explorer hoping that a link might be destroyed. Could this be a weird bug?

Build Pic

Ok I got it to work. I had to build the project using Visual Studio after I deleted those missing files. The build failed because I need to set the executable project as the startup project. I then tried to build it with Godot and the project built itself up.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.