Cannot attach Script to Unit

I’m working on the Unit Move lesson. I created the plane, the Unit empty object, added the child object, and made a Scripts folder. However, I cannot attach the Unit script to the Unit parent object.

error1


Edit: there are no console errors.

Edit 2: I changed public class NewBehaviorScript to public class Unit and that seems to have fixed it, but why did I get this error in the first place?

NB: So I paused this course and went to do the 3D essentials course that I got with it, and this error was ironically one of the first things covered. Glad It’s not just be who does this!

It looks like it’s because your class does not have the same name as your Script file. File is called Unit.cs but the class is named NewBehaviorScript. This is a common issue that can happen in Unity if you click off a newly created class before you type in the name.

Delete and recreate your class (making sure to type in the name before you click away from it) and you should be able to add it.

This was the problem, thank you. I am not used to new names/code being injected when you name/rename a script, so I wasn’t anticipating this when I hit enter after creating the script file.

You actually don’t have to delete and recreate the class. Simply changing the Class declaration to match the filename is sufficient to fix the problem. In this case, your class declaration will be

public class Unit : MonoBehaviour
{

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms