- Home /
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);
}
}
Any compiler errors? In situations where you create a script in visual studios and inherit from $$anonymous$$onoBehaviour 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.
Answer by artiumxweb · Jun 01, 2018 at 05:31 AM
The name of the class and the file must be the same KatesYu
I am having the same problem. Even though all scripts I'm running, including test scripts, have
public class example : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
with example
being the file name of the script, I'm getting the same error. What's going on? Both of the scripts I've attempted are out-of-the-box tutorial scripts and should work. The second is an empty script generated by Unity's "New Script" function in the "Add Component" drop-down. Running 2018.1.1f1.
This is the correct solution...worked for me 100%...
Answer by Rocheofpower · Jul 03, 2018 at 11:45 AM
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 :)
Adding that public class CLASSNA$$anonymous$$E : $$anonymous$$onoBehaviour {} definetely helped me thank you!
Answer by Ne0mega · Jul 19, 2018 at 04:54 AM
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 $$anonymous$$onobehaviour 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.
Ha, that was so long ago. So many troubles ahead for that poor soul. Yet, he drives on, every day. He has a dream. But thanks for re$$anonymous$$ding me of the dark times. :)
Thank you!! This was my problem as well. It would have taken a lot of time & frustration to eventually stumble across this solution, and even more time to recognize it for what it was. Thank you for saving me all that time & frustration!
Answer by vincentgravitas · Jun 09, 2018 at 07:19 AM
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.
We've had the exact same issue here, I'm convinced it's a bug.
Same issue with multiple out-of-the-box scripts. See below.
Answer by KatesYu · May 09, 2018 at 01:20 AM
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 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.
Yeah @Zeesy working later on another project I had the same issue with this error and resetting the script also did not work, perhaps it's a bug with the unity 2018.x versions
@Rocheofpower as a follow-up, I restarted the project (it hadn't gotten very far along) in a new project file and all of the scripts worked fine.
Your answer
