Script error - Can't add script behaviour AssemblyInfo. The script needs to derive from MonoBehaviour

Hiii I’m working on a 2d platformer, and written a simple script, but when I want to add it to my player, I get this error: “Can’t add script behaviour AssemblyInfo. The script needs to derive from MonoBehaviour!”

I don’t know what i’m doing wrong, but since i’m still a beginner with C#, PLS HELP!!

this is my script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed;

    private Rigidbody2D rb2d;


    // Start is called before the first frame update
    void Start()
    {  

        rb2d = GetComponent<Rigidbody2D>();
    }
   

    // Update is called once per frame
    void Update()
    {
         float moveHorizontal = Input.GetAxis("Horizontal");

        float moveVertical = Input.GetAxis("Vertical");

        Vector2 movement = new Vector2(moveHorizontal, moveVertical);

        rb2d.AddForce(movement * speed);
    }
}

Good day.

The problem is not in the script itself.

The problerm is that Unity thinks that this script doies not belong to the project.

What program are you using for scripting?

Look, If you use Visual studio, it must say the name of your project there. If says Miscelaneous files, it means is not “inside” the project

alt text

I recommend you to:

-Copy all the code of the script to a notepad.

-Delete the script.

-Close Visual Studio.

-Create a new script from Unity Editor (name it exactly the same as before: PlayerController )

-Open the script by doubleclicking it from Unity Editor.

-Paste the notepad code inside, and save.

-If this do not solve the problem, look at google about this “Miscelaneous files” issue. Its solved in a lot of posts/formus.

Good luck!

We had the same issue however the problem was that the file name wasn’t the exact same as the script name

I have the same issue, although my file is named the same as the class name and it is stored under Scripts folder of the project. Additionally my unity editor says, “Assembly has reference to non-existent assembly ‘Unity.ugui’ (Packages/com.unity.textmeshpro/Scripts/Runtime/Unity.TextMeshPro.asmdef)” Are these linked?

Follow :
Edit >> Preferences >> External Tools >> External Script Editor : Select " Visual Studio "

I had this issue, although both my class name and file name were identical. I solved it by:

Deleting the newly created script (transferring my code somewhere else to later be transposed back in).

Closing Visual Studio

Creating a new script within Unity

Might not work for everyone but I was stuck for awhile and this simple method solved my issue

I had the same problem. Turns out it was caused by another, unrelated script that caused ‘red’ error in Unity. When I fixed that error I was able to attach scripts to objects again.

Old question, but I solved this problem using the Help menu and then Reset Packages to defaults.

I had the same problem what i did was create the script inside the object and copying all the same code on that.

Incase anyone is looking at this. I’ve looked over Google and these are the fixes I found.

Check:
Filename vs class name.

Ensure there are no Compiling errors in ANY of your scripts.

Make sure the script is inside the Assets folder.