Cant Add Script - "Can't add script behaviour VisualContainerAsset",Can't Add Script "The script needs to derive from MonoBehaviour!"

I wrote this script in Visual Studio, opened by default from Unity. When trying to apply it to my background object I get the following message, “Can’t add script behaviour VisualContainerAsset. The script needs to derive from MonoBehaviour!”

What am I missing here?

My Script:

public class TiledBackground : MonoBehaviour {

public int textureSize = 32;

// Use this for initialization
void Start () {
    
    var newWidth = Mathf.Ceil (Screen.width / (textureSize * PixelPerfectCamera.scale));
    var newHeight = Mathf.Ceil (Screen.height / (textureSize * PixelPerfectCamera.scale));

    transform.localScale = new Vector3 (newWidth * textureSize, newHeight * textureSize, 1);

    GetComponent<Renderer> ().material.mainTextureScale = new Vector3 (newWidth, newHeight, 1);
}

}

The name of the class and the file must be the same KatesYu

I had this problem because I had using static UnityEngine.EventSystems.ExecuteEvents; and have my MonoBehaviour-derived class in a namespace.

The using static part was the culprit.

MonoBehaviour class in global namespace + using static = OK

MonoBehaviour class in specific namespace + using static = ERROR

I also tried putting the using static statement inside the namespace scope. It made no difference.

I am 1000% sure that this is definitely a bug.

Hey I had the same issue and fixed it by doing the following (And yes the class name and script name in unity were identical and there were no compile errors):

Select the script in the project folder
in the inspector you can see import settings, and a small cog icon, the same you see for any component attached to a game object.
Click on the cog and then select “reset” from the pop-up menu.
This worked for me :slight_smile:

So you can read my despair below, but I am almost positive this happens when you try to create new scripts when you still have scripts throwing compiler errors. You used to be unable to make new scripts when you had compiler errors showing in 2017.3. Does not seem to be the case in 2018.1.
Seems in 2018.1 you can make scripts, even if other scripts are throwing compiler errors, but once you do, all of your new scripts will suffer this error.
This project was just a few hours old, so I am just re-starting from scratch. EFF it.

Well Im hosed. None of these suggestions are working.
In fact, even if I create a brand new script, and name it WTFDUDE… …same error.
I named it that knowing it cant be having a cross reference name problem or anything.
So yeah, Ive tried a brand new project, brand new name.
First script worked.
Next one failed.
Create new c# script.
Default name… dont even rename it.
Same thing.
I seem to remember previous versions not allowing you to make new scripts if you have scripts with compiler errors.
SO perhaps this is related.
Basically, I tried to reimport some script after having to nuke a project, but there was a compiler error on one. Not sure. This is frustrating, to say the least.
2018.1
Yeah, I think that is it. Once I tear out the scripts all trying to reference each other as I try to re-build my now hosebanged project… I can make new scripts again.
Even so, I feel really sick, to say the least.

Whats the problem ? I

have the same problem

,i have the same problem. do u solve it?i use visual studio 2017.
whats wrong in my code?

I have the same problem.
I use Visual Stidio 2017 Community.
I don’t know. Also, I am a beginner.

@wharper415
Be sure to verify that the name of your script in the Unity editor is “TiledBackground”.

Thank you @Rocheofpower ; making sure that the file name matches the class and hitting reset works quite wonderfully!

I was able to fix this bug by looking at the name of the class. While the TAB up top had the correct name of the class (player.cs), this was the name in the script that was wrong.

public class NewBehaviourScript : MonoBehaviour {

The name “NewBehaviourScript” was in there, but was not the name I gave the script. It must have been a default name that didn’t get updated on create. All I know is that when I changed that name to match the name in the tab,

public class player: MonoBehaviour {

the error went away and the script stopped throwing that error.

@wharper415
public class TiledBackground : MonoBehaviour

TiledBackground and Script name have to be Identical.

My name was off by one letter, fixed it by putting in a new name at the top of the script. and hitting save scene. it refreshed on its own, also try removing script and re attaching it.

I got the same problem after I baked my project for the lighting.
I’ve been working on it for 10 days (Way too long and too much effort for a simple school project) and after I went to bake the light to have it get ready to build my rigid body first person controller gave me this error. Luckily but unfortunately I had saved and I reverted to the last save and the error (Which wasn’t there before) appeared again.
My teacher told us to do all our assignments in one project to make it easy. I went to another assignment to see if the problem displayed there; it did. I’m afraid that I’m going to be forced to start over ;~;
I checked and the error is only in my assignments project. So I can start another project folder but then I’d be even more behind in my class than I already am. WOOP WOOP!

Additionally, make sure that you are not using spaces in your file names, so ‘playerMovement’ rather than ‘player movement’. I’ve fixed this issue a number of times with this as the problem.,Also Check for any spaces in the file name, my students have been getting this error calling files ‘player movement’ rather than with no space ‘playerMovement’

For anyone finding this who already has the filename the same as the script, and has unsuccessfully reset the script using the gear icon, I had a similar error and it was related to having an Enum declared within my class. Specifically (I think) an enum that I added while also having a typo in the file name. I think unity linked the enum to the filename or something strange like that. I was able to fix the error by commenting out the enum, dropping the script onto my Gameobject like normal, (which worked!) then adding the enum back in and it worked fine. Hopefully this helps someone out one day.

I had the same problem. Someone from Unity support answered me and gave me a solution:

  • Navigate to Edit> Project Settings> Player
  • Under the ‘Other Settings’ section set the Scripting Runtime Version from ‘.NET 3.5 Equivalent (Deprecated)’ to ‘.NET 4.x Equivalent’.

In my case, it was already on Net 4.0, so I changed it to Net 3.5 and that solved the problem. The error, which I consider to be a bug, is that Unity was incompatible with the C # version and was unable to notice this. I didn’t mess with any settings, so I don’t know what made the change and caused the unavailability. I hope this help.

I created the project with the 2019 version. I was using 2018. Then I created the project back in 2018 and updated visual studio. It is currently working.