Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by wharper415 · May 03, 2018 at 06:09 AM · error message

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);
 }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RobAnthem · May 03, 2018 at 06:15 AM 0
Share

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.

15 Replies

· Add your reply
  • Sort: 
avatar image
12

Answer by artiumxweb · Jun 01, 2018 at 05:31 AM

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zeesy · Jun 28, 2018 at 04:51 PM 0
Share

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.

avatar image unity_0SzooDPCgOZTiA · Jul 06, 2018 at 09:31 AM 0
Share

That works...Thank You.

avatar image CaptainBeard97 · Jul 22, 2018 at 05:44 AM 0
Share

This is the correct solution...worked for me 100%...

avatar image
5

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 :) alt text


unity1.png (429.3 kB)
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dimoni9524 · Jul 12, 2018 at 02:55 PM 0
Share

Thank you so much, this worked immediately to me!

avatar image wechat_os_Qy07ai-eYSEl5-ONtAv6XtynA · Feb 25, 2019 at 02:22 AM 0
Share

It works for me, Thanks

avatar image kfidanci · Apr 11, 2020 at 01:03 AM 0
Share

Adding that public class CLASSNA$$anonymous$$E : $$anonymous$$onoBehaviour {} definetely helped me thank you!

avatar image
4

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image henkehedstrom · Feb 20, 2019 at 11:11 PM 1
Share

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.

avatar image Ne0mega henkehedstrom · May 23, 2019 at 02:59 AM 0
Share

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. :)

avatar image tonymotion · Jul 16, 2020 at 11:36 AM 0
Share

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!

avatar image frangagn · Jul 26, 2020 at 07:19 PM 0
Share

Oh yes! Right on man! Thanks so much!

avatar image
3

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rospeathstudios · Jun 17, 2018 at 02:56 PM 0
Share

We've had the exact same issue here, I'm convinced it's a bug.

avatar image Zeesy · Jun 28, 2018 at 07:22 PM 0
Share

Same issue with multiple out-of-the-box scripts. See below.

avatar image
2

Answer by KatesYu · May 09, 2018 at 01:20 AM

Whats the problem ? I alt texthave the same problem

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


1.png (163.0 kB)
1.png (163.0 kB)
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zeesy · Jul 10, 2018 at 05:51 PM 1
Share

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.

avatar image Rocheofpower Zeesy · Jul 10, 2018 at 08:47 PM 0
Share

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

avatar image Zeesy Rocheofpower · Jul 10, 2018 at 09:06 PM 1
Share

@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.

  • 1
  • 2
  • 3
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

111 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Fatal error! System out of memory! Unity 3D 1 Answer

OperationCanceledException: Failed to prepare target build directory. Is a built game instance running? 0 Answers

''Error Unable to initialize the Unity '' in my android apk file in Unity version 5.5.2f1 0 Answers

ArgumentException: GetComponent requires that the requested component 'List`1' derives from MonoBehaviour or Component or is an interface. 1 Answer

if statement in a get function 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges