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

    Barrett313

    Joined:
    Feb 20, 2019
    Posts:
    5
    Because the script class cannot be found. Make sure that there are no compile errors and that the file name and class name match

    I have 100% made sure the names match. I never changed the names to begin with. I can not resolve this to add scripts to anything inside unity. Please help.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,143
    So, the script name matches exactly? And not something like PlayerManager and playerManager?
    The script inherits from Monobehaviour?
    And you're not seeing any compile errors in the console? (this is possibly your issue if you can't add any scripts to gameobjects)
     
    ilaibatman, hoos87, bcaoradea and 2 others like this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You need to fix any compile errors in your code before Unity can compile any changes to code (or new scripts) that have been made. Unity has to have compiled your scripts before it will be able to add them as components of GameObjects.

    What errors does your console say you have?
     
    SevenMD, kaip7286 and paul_unity171 like this.
  4. Barrett313

    Barrett313

    Joined:
    Feb 20, 2019
    Posts:
    5
    No errors in console. File name and class match. I’m trying to get my son started on creating his first game in Unity, he’s 11. I made a simple controller for his character and it’s named “PlayerMovement” class is “PlayerMovement” as well. I’ve tried renaming, restarting, recreating the controller entirely. Nothing seems to be working. This only started after the new update. I even uninstalled and reinstalled Unity. I really don’t know what else to do.
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Can you post this PlayerMovement class here?
     
  6. Barrett313

    Barrett313

    Joined:
    Feb 20, 2019
    Posts:
    5
    This is the new controller I just started again. I’m trying to show him how to test as he goes and what all this means. But as I try to attach it to his character I get the error. Nothing in console. Names match.
     

    Attached Files:

  7. Barrett313

    Barrett313

    Joined:
    Feb 20, 2019
    Posts:
    5
    I’ve just never had this issue in the past and I feel like it’s something simple that I’m missing. And I know I’ll feel like an idiot when I find it, but man I’ve been at this for days now.
     
    Fadnd likes this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Things to try:

    1. Make sure the script is not located in a folder called Editor.

    2. Copy the script physically away to another part of your hard drive, DELETE the copy in the project, then make a fresh blank PlayerMovement.cs script. If you can't add that then the problem is somewhere else.
     
  9. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Are you sure you don't just have console errors disabled in the console?
     
    Fizzget, Bunny83 and Kurt-Dekker like this.
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    What @Joe-Censored means is this button in the upper right corner of your console window: make sure it is ON and does not show any errors:

    (Same goes for Log, LogWarning, LogError, those three buttons)

    errors.png
     
    Last edited: Oct 21, 2021
  11. Barrett313

    Barrett313

    Joined:
    Feb 20, 2019
    Posts:
    5
    There’s no console errors. Though I’m leaning toward the issue being with PC, I haven’t had this problem with MAC. May be an issue with my desktop in particular, I don’t know. But I’m giving up. Thanks for the input.
     
    CloudyVR likes this.
  12. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,143
    One thing to try is export your assets as a unitypackage. Than start a new project and import the assets.

    I know it sounds strange, but once we had an animation that no matter what we did was screwed up. Luckily it was a small project so I exported the scene, started a new project and suddenly there were no animation issues.

    Doubt it's a "pc" issue directly.
     
    Bunny83, StonedAnt and JBR-games like this.
  13. mwsbkru

    mwsbkru

    Joined:
    Apr 19, 2019
    Posts:
    1
    I had the same issue, in my case the problem was solved by deleting directory "Library\PackageCache".
     
  14. Yohansandun

    Yohansandun

    Joined:
    Dec 24, 2018
    Posts:
    5
    I had the same issue.
    Problem was solved after installing required .NET SDK and runtime framework.

    Unity 2019.2.15f1 requires .NET 4.7.1

    Hope it helps.
     
    g1o2r3z4o5v and thedreamitseems like this.
  15. Boolai

    Boolai

    Joined:
    Nov 13, 2014
    Posts:
    9
    I had the same problem. I found that some unrelated script had an error. Once fixed I could add scripts to object again.
     
  16. PandaS2005

    PandaS2005

    Joined:
    Apr 1, 2020
    Posts:
    1
    sir can you tell exact location
     
  17. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    In Unity where your project is located (outside of Unity!) there are the
    Assets
    and the
    ProjectSettings
    folders...
    Library
    is right alongside, and
    PackageCache
    is underneath Library.
     
  18. Asa_Iwasa

    Asa_Iwasa

    Joined:
    Apr 24, 2020
    Posts:
    1
    If anyone still has this issue you might want to try:
    Go to the "Window" menu
    Then down to "Package Manager" and click
    You will get a list of every package available. Change from all available to just in project to shorten the list
    Next click on the "package manager UI" from the list
    Finally click "remove" from the lower right corner
     
    Imbruno likes this.
  19. Blijzijn

    Blijzijn

    Joined:
    May 8, 2020
    Posts:
    7
    Idk what is wrong but it still doesn't work. Am i this stupid or is it just Unity? im literally trying to fix it for like 2 hours now and it is stil not working. Maybe my Visual Studio needs to be on the same disk as Unity or something but im going to stop trying....
     
  20. bb8_1

    bb8_1

    Joined:
    Jan 20, 2019
    Posts:
    98
    This didnt work for me too(for Netcode simple example from Manual). I did this actually : i saved first wrong name of the script and opened it in vs 2019. Then i Renamed it in the editor(and again opened it in vs 2019) and it didnt work. Then i deleted C# script and created new one renamed it and then it all worked good. Basically it seems if it is renamed 2 times somehow system cannot find the file dont know why - bug probably.

    So just create file rename it as u want and then open in vs 2019 and it works for me - i use latest .net, windows 10 => also both unity and projects are on the same disk etc though do not know is that related(other posters above wrote about this) - hope this helps
     
    Last edited: May 10, 2020
    dodo-12-37 and Thesnake66six like this.
  21. beerinbox

    beerinbox

    Joined:
    Feb 29, 2016
    Posts:
    26
    Got the same error today, but it also said something along the lines of "Can your Unity install be corrupt?" So, if you just updated your engine, the install might be corrupt. If you did not, I do not know the reason.
     
  22. Blijzijn

    Blijzijn

    Joined:
    May 8, 2020
    Posts:
    7
    Do you also need to download MonoDevelop on your pc or is that already in Unity?
     
  23. Thesnake66six

    Thesnake66six

    Joined:
    Apr 3, 2020
    Posts:
    1
    This seemed to work for me! Thanks!

    Edit: Having the same problem. Different script, and I used the same solution. Didn't work. Feels like the error is getting smarter; every time we fins a solution it 'evolves' so that the solution doesn't work anymore.

    Edit 2: Just noticed that in the inspector the script has a speech bubble (noob coding language terms here) saying 'No MonoBehavior scripts in this file, or their names do not match the file name.'
     
    Last edited: May 15, 2020
    bb8_1 likes this.
  24. NotAnonymoose

    NotAnonymoose

    Joined:
    May 18, 2020
    Posts:
    1
    Ive been having the same problems. I have no idea what to do as i have just started with unity. Please help me.

    I tested by just creating a c# script i named it "Test Script" and dragged it into my player as a new component. It now displays the message, "Can't add script component 'Test script' because the script class cannot be found. Make sure that there are no compile errors and that the file name and class name match." What do i do now?
     
    Last edited: May 18, 2020
    UptownFall likes this.
  25. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,143
    If you named the script "Test Script" then you may have an issue because class names can't have spaces. I'd make sure your class name doesn't have a space which could also lead to compiler errors.
     
    bb8_1 likes this.
  26. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,143
    I was curious what happens when you create a script with a space as I'd never done it. So here it is. As you can see, the script is created with a space, but in the code it doesn't have the space. It then says at the top, no MonoBehaviour scripts in the file or their names do not match. No compiler errors were thrown, but as suspected, you can't add the script to a gameobject as it can't find it due to the name mismatch.

    upload_2020-5-18_10-36-53.png
     
    Marko269s, Phaeroh and bb8_1 like this.
  27. Hungry-Lion

    Hungry-Lion

    Joined:
    Sep 2, 2018
    Posts:
    21
    Ok I am pissed really. So the standard assets are fracked really, the editor folder cause problems, so I moved that away from the standard assets, then I accidentally created a script in that piece of S*** editor folder and moved it away and bam every script I have made stopped working.

    I have been coding for 15 years so don't give S***e like your names don't match or you can't have spaces ok, I have been working on this project for 4 months now and my progress is fraking wasted.

    I seriously want to drinik the blood of the guy responsible for this!
     
    Jhota92 and Arska1910 like this.
  28. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,143
    First, we understand you are upset, but being upset on the forums just to complain will not get you any help.

    Second, I've been coding a lot longer than you. Unity does have a few of it's own quirks and no matter how long you've been coding, I doubt you've written perfect code every time. I sure as heck haven't.

    Third, if you want help, ask for help instead of trying to attack people who could potentially help you even before you try to get help, because I seriously doubt your work is wasted.

    And of course, as long as I've been coding, the best thing I ever did was start using source control. In the world of programming, there isn't such a thing as "everything always goes right". So the ability to roll back or access a previous commit is invaluable.
     
  29. Hungry-Lion

    Hungry-Lion

    Joined:
    Sep 2, 2018
    Posts:
    21
    Yeah you ain't helping. How can you, I see this thing persist since version 5 in various manifestations. Hey Unity-chan Instead of tech dem...erm fmvs try making a game once in awhile, you may find why your stable versions are not so stable after all.
     
  30. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    This is so unnecessary. It's 2020 (edit... 2022!)... please join us and install and use source control. Unity3D works really well with git and that combination saves my bacon almost every single day. Once you start using source control, your actual productivity goes up because you can fearlessly rip-tear-shred to try stuff and see how it goes, knowing you can always roll back. It is the ultimate peace of mind.

    EDIT to add my "standard source control blurb:"

    I'm sorry you've had this issue. Please consider using proper industrial-grade enterprise-qualified source control in order to guard and protect your hard-earned work.

    Personally I use git (completely outside of Unity) because it is free and there are tons of tutorials out there to help you set it up as well as free places to host your repo (BitBucket, Github, Gitlab, etc.).

    You can also push git repositories to other drives: thumb drives, USB drives, network drives, etc., effectively putting a complete copy of the repository there.

    As far as configuring Unity to play nice with git, keep this in mind:

    https://forum.unity.com/threads/prefab-links-keep-getting-dumped-on-git-pull.646600/#post-7142306

    Here's how I use git in one of my games, Jetpack Kurt:

    https://forum.unity.com/threads/2-steps-backwards.965048/#post-6282497

    Using fine-grained source control as you work to refine your engineering:

    https://forum.unity.com/threads/whe...grammer-example-in-text.1048739/#post-6783740

    Share/Sharing source code between projects:

    https://forum.unity.com/threads/your-techniques-to-share-code-between-projects.575959/#post-3835837

    Setting up an appropriate .gitignore file for Unity3D:

    https://forum.unity.com/threads/removing-il2cpp_cache-from-project.1084607/#post-6997067

    Generally setting Unity up (includes above .gitignore concepts):

    https://thoughtbot.com/blog/how-to-git-with-unity

    It is only simple economics that you must expend as much effort into backing it up as you feel the work is worth in the first place.

    "Use source control or you will be really sad sooner or later." - StarManta on the Unity3D forum boards
     
    Last edited: Apr 22, 2022
  31. Jeea84

    Jeea84

    Joined:
    Aug 25, 2017
    Posts:
    15
    If I delete this folder, won't it also delete my packages? I'm already facing different issues with Cinemachine and Probuilder components.
     
  32. Jeea84

    Jeea84

    Joined:
    Aug 25, 2017
    Posts:
    15
    And which framework does the newest version require, anybody know? New is to say, 2019.5.1f1
     
  33. BoomerTheStar47_2

    BoomerTheStar47_2

    Joined:
    May 21, 2020
    Posts:
    1
    Felt like telling you thanks, because that's the exact solution I needed!
     
    Soareverix likes this.
  34. JevonDanaristo

    JevonDanaristo

    Joined:
    Jul 23, 2020
    Posts:
    1
    Just solved my problem, If you name your folder scripts or script change that now. mine is working after renaming it to f**kton
     
  35. SheZii

    SheZii

    Joined:
    May 26, 2016
    Posts:
    4
    I had a similar problem. My problem was, one of my scripts was in plugins folder, and i was trying to access another script into that script which was not in Plugins folder... So moving that script out of Plugins folder solved my problem.
     
  36. feelysean15

    feelysean15

    Joined:
    May 6, 2020
    Posts:
    1
    I was trying for ages to find the issue and this was it thanks!!
     
    Soareverix likes this.
  37. ahmedaniss

    ahmedaniss

    Joined:
    Sep 18, 2019
    Posts:
    98
    problem fixed here :
     
    duine, losersock, kostolomova and 3 others like this.
  38. leejponce

    leejponce

    Joined:
    Nov 4, 2017
    Posts:
    1
    I've already tried that it doesn't work. I did move unity in a different hard-drive and visual studios in a same hard-drive. Now i'm having this problem too. I have copied pasted and reinstalled and uninstalled. Right now I am uninstalling everything and moving it to the original hard-drive I was using. I'm going to see if that solves my problem. What good is unity when you can't make any simple scripts work?
     
    Marko269s and brownbomber like this.
  39. brownbomber

    brownbomber

    Joined:
    Jul 15, 2017
    Posts:
    4
    I've gone through this whole thread looking for help on this problem, I'm just about to give up!! This is torture! Lol
     
    vuokkolehtola likes this.
  40. brownbomber

    brownbomber

    Joined:
    Jul 15, 2017
    Posts:
    4
    I don't know if anybody else is having this problem, but after looking around online some more, someone suggested uninstalling from the package manager, "Rider Editor", I deleted it and it automatically fixed all compiler errors, all scripts are working without any adjustments to naming in the code!! I hope this helps, cause it sure as hell gave me a big relief!! :)
     
    Lemonnadeislemon likes this.
  41. whatisthislol

    whatisthislol

    Joined:
    Jul 20, 2020
    Posts:
    1
    Hello I have the same issue. I've tried all the suggestions but it still wont work :(
     
  42. Underbane_Productions

    Underbane_Productions

    Joined:
    Sep 26, 2020
    Posts:
    1
    No disrespect to anyone above, but the original issue is regarding a new script not being attached as a component. This likely is caused by an outdated editor.

    If you have this issue, and nothing above resolves it for you, please try the following:
    • Navigate in the Unity Menu to "Window"
    • Select "Package Manager"
    • Make sure "In Project" is selected under "Packages"
    • In the "Unity Technologies section, look to see if all items listed there are up to date. You can tell if something is up to date because it will have a green checkmark.
    • If something is not up to date, it will have a circle with an up-facing arrow to the right of it. Click this, then install the update.
    • After waiting a few moments, navigate back to your script folder and delete the script that was causing the issue. (If you have data here you need, instead open it, copy and paste it into a new script)
    • Afterward select the Object/item/etc you wish to add the script to and drag the script to it. Or access the inspector and add as a component.
    • If the problem persists, make sure you have the latest version of unity installed.
    upload_2020-9-25_22-6-53.png
     
  43. Cloudless-Rain

    Cloudless-Rain

    Joined:
    Jun 24, 2015
    Posts:
    12
    I had this issue with a band new, empty script. I created in the Project window then got the 'cannot find class' window when I tried adding it to a gameobject. In my case since there was nothing in the script yet I simply deleted it and made a new one. The new one worked, go figure.
     
  44. ian_maurmann

    ian_maurmann

    Joined:
    Jan 6, 2020
    Posts:
    3
    For me, it was caused by an error in another file. Weird.
     
  45. codenine69

    codenine69

    Joined:
    Nov 5, 2020
    Posts:
    1
    [QUOTE = "ahmedaniss, publicación: 6256661, miembro: 3696869"] problema resuelto aquí: [MEDIA = youtube] dx35_RNnJEQ [/ MEDIA] [/ QUOTE]

    funciono gracias !!!
     
    savannahroot likes this.
  46. savannahroot

    savannahroot

    Joined:
    Dec 1, 2020
    Posts:
    1
    Thanks, this worked !!
     
  47. OnkelAtonkelton

    OnkelAtonkelton

    Joined:
    May 4, 2013
    Posts:
    2
    Hi everyone!

    Seems like Unity does not like it when you rename the file (and or class) while it is referenced in other files.
    I followed the 'Ruby's Adventure: 2D Beginner' course and decided to rename my 'Controller' to 'RubyController' mid way through the course and got the exact same error.

    No deletion, updating or anything helped.
    What DID help:
    Walk through every file with your socks off and find each and any reference to my old 'Controller' and replace it with 'RubyController'. Hey presto! Suddenly Unity decided to like my scripts again and happily run along.

    I hope that helps somebody!

    Wild greetings,
    Madweasel
     
  48. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,143
    Or, if you use the VS rename feature, you can rename all the values at once if they are properly referenced.
     
  49. OnkelAtonkelton

    OnkelAtonkelton

    Joined:
    May 4, 2013
    Posts:
    2
    Well said. If I were a sane person I would do that.
     
  50. unity_f_Xi8Kh1pfrAnA

    unity_f_Xi8Kh1pfrAnA

    Joined:
    Jan 6, 2021
    Posts:
    1
    This worked for me.
     
Thread Status:
Not open for further replies.