- Home /
"can't add script behavior assemblyInfo.cs. The script needs to derive from Monobehavior"
I found this in another question, but nobody there had the answers. I am making a 2d sidescroller platforming game with some of my friends, and this error appears. The previous answers said to make sure it is in the right project and that there are no errors in the project, and I have satisfied both of those solutions to no avail. Also, when i click on the script in unity and the inspector window pops up, it says "No MonoBehavior scripts in the file, or their names do not match the file name" even though the name of the script and the name of the class are the same. Here is my script code, if it is any help:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class parallax : MonoBehaviour
{
private float length;
private float startpos;
public float parallaxEffect;
public GameObject cam;
// Start is called before the first frame update
void Start()
{
startpos = transform.position.x;
length = GetComponent<SpriteRenderer>().bounds.size.x;
}
// Update is called once per frame
void Update()
{
float dist = (cam.transform.position.x * parallaxEffect);
transform.position = new Vector3(startpos + distance, transform.position.y, transform.position.z);
}
}
I am not super good at C#, especially with all of the extra things added by unity, so I dont know exactly what the problem is. Thanks in advance!
Answer by dvoigt · May 24, 2020 at 01:13 AM
@billygoatcheese25 . I have run into this type of thing and usually it is that I saved under a different name originally and then changed it. Or, I moved the script from one place to another and it doesn't see it correctly now. What I would do is copy the contents of the script, delete the entire script and go back into Unity and create a new script where you want it. Name it the exact name you have, "parallax" in this case. Then erase everything from that script and paste what you copied previously. Save it, and it should compile. The class name should start with an upper case letter as Jasr_88 said.
Answer by dvoigt · May 26, 2020 at 04:14 PM
,I have run into this type of thing and usually it is that I saved under a different name originally and then changed it. Or, I moved the script from one place to another and it doesn't see it correctly now. What I would do is copy the contents of the script, delete the entire script and go back into Unity and create a new script where you want it. Name it the exact name you have, "parallax" in this case. Then erase everything from that script and paste what you copied previously. Save it, and it should compile.
Also, your class name is usually capitalized if you are following convention.
Answer by Jasr_88 · May 23, 2020 at 09:06 PM
As the message says, you just need to rename your file to match the class name (in this case the file must be named parallax.cs instead of assemblyInfo.cs)
By the way the correct way to name Classes is starting with a Upper case letter (Parallax)
As i said already, the name of the file and the name of the class are the same.