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);
}
}
Any compiler errors? In situations where you create a script in visual studios and inherit from MonoBehaviour afterwards, if there are compiler errors in any other script, it will not compile any new scripts, and thus will register the class file as an empty class.
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
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.
I am on this website pretty often but I have never signed in. I did it now just to thank you for helping me. I knew for sure that the name was correct and that it derived from Monobehaviour because I created a bunch of new scripts just to test. After some time my other scripts started to get the same error but your solution solved it, you have to take care of the errors before creating a new script. I have no idea what Reward user does but I gave you my point.
I am having the same problem. I tried to reset as @Rocheofpower described, but I am getting the same error. Script name and class name are the same. This is true even for the default script made by Visual Studio.
Lots of people are saying it is because the names aren't matching. Truth is, if you have ANY scripts that have compiler errors, you can not add any more scripts to your project. If you try, this error gets thrown. having class names that do not match script names would cause compiler errors, so that is why people are saying making sure the script name and class names matching worked, because if they don't match, you get a compiler error.
@wharper415
Be sure to verify that the name of your script in the Unity editor is “TiledBackground”.
I would recommend removing the shader from your project, keeping it somewhere else, upgrading materials and then re-importing the shader after it is back to normal.
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.
Any compiler errors? In situations where you create a script in visual studios and inherit from MonoBehaviour afterwards, if there are compiler errors in any other script, it will not compile any new scripts, and thus will register the class file as an empty class.
– RobAnthemThanks for the fast answer. But how can I do this?
– Sagan200