Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Can’t add script component...

Discussion in 'Scripting' started by Barrett313, Feb 20, 2019.

Thread Status:
Not open for further replies.
  1. cheelee168

    cheelee168

    Joined:
    Jan 6, 2021
    Posts:
    1
    I am completely new to all of this and ended up having the same problem. Tried a lot of the above and nothing worked except for one post that said it was an error on another script. On closer inspection, I noticed errors on a script I created for 'wheels' from a tutorial I was following. Once those errors were corrected (my fault for typos) I could add scripts like before.
     
  2. Ensortainment

    Ensortainment

    Joined:
    Dec 19, 2020
    Posts:
    14
    I wasn't able to attach any scripts to anything just now. I had a couple scripts open in vs code from another project which I was referencing and once I closed out of those everything seemed to be fine again.
     
  3. gamer_maker_123

    gamer_maker_123

    Joined:
    Jan 17, 2021
    Posts:
    2
    I recently started unity and when i type simple code such as Debug.Log(" hello ");
    it says error too many characters in character literal and when i drag the script to my player cube it dosent work s help
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,531
    No, the error you quoted here means you had used something like this
    ' hello '
    instead of this
    " hello "
    The single quotes denote a character literal and not a string literal. A character literal defines a single char value. Some char values require an escape sequence but still denote a single char. For example
    '\n'
    . This is a single line feed character. Strings are sequential arrays of chars. You can read individual chars of a string like in an array:

    Code (CSharp):
    1. string myString = "Hello World!";
    2. char c = myString[6]; // c will contain 'W'
    3.  
    As it was said countless of times, if any of your code contains syntax errors, none of your code will be compiled. If your code is not compiled properly you can not use it. So make sure you don't have any syntax error in your Unity console.

    If you need further help, please do not hijack other ancient threads. Create your own thread and include your code that doesn't work. Note when posting code you have to use code tags here in the forum. There's a sticky thread in this subforum explaining how.

    Though before carrying your issue to the community, make sure you put some efford into solving it yourself. Note that while the scripting forum is for any questions regarding scripting in Unity, it's generally not meant to answer basic C# syntax related questions. This is not a tutoring platform for learning basic C#. Imagine the difference between a poetry class and an english class. In a poetry class a student would not expect to learn basic english. Programming languages are like normal human languages. Though they generally are much simpler but also have zero tolerance for errors. While everyone can understand Yoda's messed up grammer, a computer does not understand you, if you don't use proper syntax and grammer.
     
  5. sacreative10

    sacreative10

    Joined:
    Feb 1, 2021
    Posts:
    1
    I had the same error but turns out my main class in the c# script was different from the filename itself. Please check that. Please check the photo for correspondence.

    upload_2021-2-2_17-2-18.png
     
    tscitch likes this.
  6. rpg_developer

    rpg_developer

    Joined:
    Feb 8, 2021
    Posts:
    3
    Got it too, and I solved it by upgrading to 2020.2.3. Great!
     
  7. g1o2r3z4o5v

    g1o2r3z4o5v

    Joined:
    Mar 9, 2021
    Posts:
    1
     
  8. GamingDevlopers

    GamingDevlopers

    Joined:
    Jan 14, 2021
    Posts:
    1

    But when i made a new script component inside inspector it also not work
     
  9. rileydj2009

    rileydj2009

    Joined:
    Apr 20, 2021
    Posts:
    38
    thank you good sir i just looked how to fix this and you saved me
     
  10. skycat2216

    skycat2216

    Joined:
    Sep 25, 2020
    Posts:
    3
    Everyone, Every errors(the number of error is zero) need to be corrected
     
  11. parpanchi77

    parpanchi77

    Joined:
    Apr 13, 2021
    Posts:
    1
    Why does the program give this error?
     

    Attached Files:

  12. ohthepain

    ohthepain

    Joined:
    May 31, 2017
    Posts:
    100
    I'm getting the same thing. Eight years after this thread started. What a piece of crap. I did use the VS rename feature.
     
  13. ohthepain

    ohthepain

    Joined:
    May 31, 2017
    Posts:
    100
    Sure, we will just rewrite our entire game to work with the latest Unity just to load a script LOL
     
  14. ohthepain

    ohthepain

    Joined:
    May 31, 2017
    Posts:
    100
    So I deleted "Library\PackageCache" and got the game to recompile and run. I still couldn't attach the renamed script. So I tried updating to the latest LTS. Too many breaking changes between 2020.3.5f1 and 2020.3.8f1. So I went back to 2020.3.5f1. Still can't add the script. In the end I renamed the file back to it's original name and reattached it.

    So I guess the moral of the story is, get your filenames right the first time! You shouldn't be renaming files!

    WHAT A PIECE OF CRAP!!!
     
    Last edited: May 21, 2021
  15. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,913
    The real moral story of course that you should know the software you're working with. You only should move, delete, rename files inside Unity and always keep your filename and class name in sync. This isn't exactly a trade secret.
     
    Bunny83 likes this.
  16. ohthepain

    ohthepain

    Joined:
    May 31, 2017
    Posts:
    100
    Unity won't rename your class, but thanks for your "help".
     
  17. techtreepishield

    techtreepishield

    Joined:
    Feb 19, 2021
    Posts:
    2
    i corrected the file name and the class and it worked
    :)
     
  18. Koehl01

    Koehl01

    Joined:
    May 15, 2021
    Posts:
    2
    A little late to the party but had the same issue. If you click off of the new script before changing the name it will be set to NewBehaviorScript. Even if you change it afterwards, in your code it will still be NewBehaviorScript. Update the class name to match whatever new name you give it in Unity and it should work. Hope this helps someone!
     
  19. Calimar41

    Calimar41

    Joined:
    Nov 26, 2020
    Posts:
    5
    Had a similar error - turns out you shouldn't put script in a "editor" folder (and ok...) but you shouldn't put them in a folder named "library" (within the assets folder of course) either.
    Or at least, it started working as soon as I moved the file outside the "library" folder.
    Just in case someone else meets this.
     
  20. s9070698e

    s9070698e

    Joined:
    Nov 11, 2015
    Posts:
    3
    Yup I solved mine also. Just transfer the affected script to a different folder. Done... Solved yay.
    Hahaha precious 15mins and momentum gone xD.
     
    Calimar41 likes this.
  21. Huxii

    Huxii

    Joined:
    Sep 15, 2018
    Posts:
    32
    Had the same problem several times. What helped me is to rename the script file (not the class), let compile, then rename back and let compile again. Then it works.
     
  22. Fruitfly08

    Fruitfly08

    Joined:
    Feb 5, 2021
    Posts:
    69
    I know this tread is a bit old, but just in case someone finds it again, all I had to do was copy the entire script, so I have it saved, then delete the script, create a new script with correct naming, so it matches with the class, paste everything, then it works just fine, I know Unity has some bugs where it will save things that you can't see, such as a non-serialized variable in the inspector having the wrong value saved even though you didn't edit it, seems like it was the same issue this time too for me, I did have a complier error in the script, but even though I had fixed it Unity still thought a compiler error was there, which is why it didn't work until I deleted everything entirely.
     
    BayouSoftware likes this.
  23. loremipsumdolorsit

    loremipsumdolorsit

    Joined:
    Sep 14, 2021
    Posts:
    2
    The same thing happed to me too and i checked it for errors
     
  24. linkandcheezer

    linkandcheezer

    Joined:
    Jul 4, 2020
    Posts:
    2
    This is what fixed my issue. I'm a new user and I was having this issue on my fresh build but I figured out what was going on for me.
    upload_2021-10-10_22-1-29.png
    Make sure before you create a new script that there aren't already any compile errors caused by any current scripts. If there is a compile error then when you make a new script it won't work correctly and it'll display the little MonoBehavior issue pop up at the top of any new script, if you reimport it'll display at the top of every script. Make sure to check your console frequently when building

    The little red notif doesn't pop up unless you click the Console.
     

    Attached Files:

  25. Aglovale_

    Aglovale_

    Joined:
    Dec 1, 2021
    Posts:
    1
    Go into your script (double click it) at the top u should see New behavior : MonoBehavior

    Change "NewBehavior" to the script name (the thing you double clicked)
     
  26. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    If you create the script in Unity and name it at the time of creation, your class name will match. If you simply hit enter to confirm "NewBehaviourScript", then you rename it, then yes, your class name will not match. Simply name it when you first create it and you'll not have issues.
     
    Bunny83 likes this.
  27. Ajosic

    Ajosic

    Joined:
    Sep 1, 2021
    Posts:
    3
    Just spent a couple of days with a similar issue, a ScriptableObject instance lost it's script.
    I haven't moved/renamed or did anything in git with either the script or SO instance in order to cause it.

    Tried reimporting the files in question, then the whole project which didn't help. Got to the end of this thread while the project was loading at the start of a new work day, lo and behold, the script is no longer missing. Not sure how to feel about it :D

    We do have a project with a massive amount of SO objects and assets, reimporting lasts for a couple of hours (sometimes getting stuck and spinning indefinitely), so at this point I'm assuming that there's some unreliable imports that happen once in a while. Reading the "rename, recompile, rename back to old name" workaround would also suggest something like that is happening?
     
  28. xPatrick

    xPatrick

    Joined:
    Mar 30, 2019
    Posts:
    1
    Thank you very much. I'm a beginner, I rename my script directly from Unity and forgets about renaming it from the script itself. This was such a simple solution yet I got no idea how to fix this. I wonder why unity didn't add simple features to rename automatically from unity into script name also:eek:
     
  29. CopyPasterCoder

    CopyPasterCoder

    Joined:
    Aug 23, 2021
    Posts:
    1
    Is there any update on this? I’m a 11 year old (I know not the ideal age) making a basic 3d game and I accidentally made a script with a space, I fixed it and made sure no bugs were in any script and my class name is accurate for all files and led to no change. I’m getting the same error as OP and not sure how to fix it. None of the above solutions have done anything, removing bugs, resetting packages in help menu, reimporting packages, clearing package cache, changing my Unity version from 2020 LTS to 2021 latest (I don’t have the exact version numbers, etc.. Hope some staff from Unity can check this out.


    EDIT: I fixed the error by fixing a bug in another script. Ooops.

    Some specs that are probably unrelated:

    Visual Studio 2019 (latest)
    Unity hub 3.0.0 Beta (latest)
    Unity editor 2020/2021
    Windows 11 (latest)
    MacBook Pro 2015 early (Bootcamp)
    8gb Ram 500gb ssd (half for windows, half for macOS)
     
    Last edited: Jan 19, 2022
  30. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Update your hub to the full release, since it's not in beta I guess anymore.
    Your class name and the script name can't contain a space. So make sure neither do.
    Restart your mac, make sure you've shut down Unity completely, visual studios completely, etc.

    Also remember caps matter, so make sure you script name and class name match.
     
  31. BluezamX

    BluezamX

    Joined:
    Mar 30, 2018
    Posts:
    8
    For me simply deleting the script's .meta file with GitHub Desktop fixed the issue. I got a message about reloading scripts and everything was fixed.
     
    OleksandrMartysh likes this.
  32. maxmcintosh636

    maxmcintosh636

    Joined:
    Jun 2, 2021
    Posts:
    1
    remove spaces worked for me
     
  33. gchamorro_J_

    gchamorro_J_

    Joined:
    Jan 2, 2022
    Posts:
    1
  34. bkaiorius

    bkaiorius

    Joined:
    Feb 7, 2022
    Posts:
    1
    apaguem o script novo que tentaram associar ao objeto, acontece que simplesmente o script acaba se corrompendo e causa um problema na compilação infinito, apaguem o script criado que voltará ao normal.. por causa de um script o jogo inteiro não funciona pois, se houver um script com este erro, automaticamente todos se corrompem
     
  35. Brain_nguyen

    Brain_nguyen

    Joined:
    Feb 9, 2022
    Posts:
    4
    Hello can anybody help me please Im trying to put a script in and it says this upload_2022-2-9_20-58-27.png
     

    Attached Files:

  36. Brain_nguyen

    Brain_nguyen

    Joined:
    Feb 9, 2022
    Posts:
    4
    And I Also have 19 errors upload_2022-2-9_21-1-55.png
     
  37. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Yep, fix your compile errors. You even have a list of them in your console.

    My suggestion is to start your own post, share your code with code tags, share your errors (copy and paste them from the console.)

    Help others to help you. And from the looks of the errors, my guess is some of those may even be simple typos. But, start your own post and be constructive in providing information to help you.
     
  38. Brain_nguyen

    Brain_nguyen

    Joined:
    Feb 9, 2022
    Posts:
    4
    I don't understand how to fix
     
  39. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,895
    All those errors in your console are because you have a script with syntax errors. AKA, it isn't valid C# code. They need to be fixed before you add a component.

    If you're following a tutorial, check back on it and follow it closer. If you're coding on your own, make a forum post and we can point out where the errors lie. That or do some beginner C# tutorials.

    Your coding syntax needs to be 100% correct or it won't compile.
     
  40. Brain_nguyen

    Brain_nguyen

    Joined:
    Feb 9, 2022
    Posts:
    4
    Thank you u helped
     
  41. Juicydemondog

    Juicydemondog

    Joined:
    Apr 16, 2022
    Posts:
    1
    hello i had the same problem but fixed it ill tell you how: so i had a c# script named Player Contoller there was no problems but inside the script it said PlayerController if i changed it to PlayerContoller 4 errors would pop up so at this point i had nothing inside the scrpit so i deleted it and changed the name to PlayerController and that worked for me
     
  42. melio_kombo

    melio_kombo

    Joined:
    Apr 29, 2022
    Posts:
    1
    when you did the solution above did u check the spaces and characters too? i sis that and it fixed the problem
     
  43. OnatKorucu

    OnatKorucu

    Joined:
    Apr 18, 2018
    Posts:
    8
    In my case, the script was outside of the Assets folder. Moving it into Assets/Scripts solved the issue.
     
Thread Status:
Not open for further replies.