Search Unity

The script don't inherit a native class that can manage a script

Discussion in 'Scripting' started by HumphreyGames, Nov 25, 2020.

  1. I have 2020.3.8f1 installed, never happened anything like this and I have 2020.3.X for quite some time now.

    Now I'm installing 2020.3.6f1 again, will see what the tests show. But I had it at some point, and two of my projects were on it at the time, never happened anything like it. I really think it's not Unity (or something super-rare constellation of things).
     
  2. FierceElemental

    FierceElemental

    Joined:
    May 26, 2021
    Posts:
    1
    I fixed that error by making sure that both the name and the Class were the same, just try checking that before doing anything else.
     
    moosesnWoop likes this.
  3. moosesnWoop

    moosesnWoop

    Joined:
    Sep 12, 2019
    Posts:
    12
    I had the same issue, then I saw that the name of the script and the name in the editor were different. Make sure they have the same name.

    If you are 100% sure that's correct, first close Visual Studio/Code then head over to preferences > external tools > regenerate project files.
     
  4. Shrit1401

    Shrit1401

    Joined:
    Oct 25, 2020
    Posts:
    1
    Heyy I got the same problem but solution is really simple no need no even quit unity editor

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraFollow : MonoBehaviour
    6. {
    7.     public Transform player;
    8.     public float smoothSpeed;
    9.     public Vector3 offset;
    10.  
    11.     private void FixedUpdate()
    12.     {
    13.         transform.position = player.position + offset;
    14.     }
    15. }
    16.  
    in this code the name you can see

    Code (CSharp):
    1. public class CameraFollow : MonoBehaviour
    you should have no gaps while writing the script name, In the editor I made a silly gap in between which was unacceptable

    Conclusion:
    don't put gap while naming your script, THANKS :)
     
    PerthBoy likes this.
  5. MuhamadOsamaJaved

    MuhamadOsamaJaved

    Joined:
    May 29, 2021
    Posts:
    1
    Hello everyone. So a majority of people seem to have this error. I don't know if someone is still looking for the fix or not. I faced this error and many other errors regarding scripts. So, I figured out a solution to this problem that works for me at least and I hope it works with others as well. So the fix is simple. When you are editing a script, close all other scripts that may have been open in visual studio and the only script open should be the one you are working on. That way, the error should disappear.
     
  6. MrPixelBoss

    MrPixelBoss

    Joined:
    May 22, 2021
    Posts:
    1
    Hello I think you may have renamed the script after making it
     
    Maxwal likes this.
  7. TheCesarDUDE

    TheCesarDUDE

    Joined:
    Apr 10, 2021
    Posts:
    1
    Hi, Sorry for being late. For me, I had imported a few things and there was an error message in the console. I just deleted the entire 'scripts' folder which housed the scripts giving me the errors. Once I did, Unity did the whole C# recompile thing and I could then add my scripts without any problem. Hope this helps
     
  8. russmac316

    russmac316

    Joined:
    Apr 27, 2021
    Posts:
    5
    I'm not sure if this will help anyone, but when I had this issue, I believe it was Visual Studio's fault. It named the public class incorrectly as "NewBehaviourScript" instead of the name of the script I created in Unity. Also, this likely would happen if you rename the Script in Unity but not in your C# editor.
     
  9. simon_rosenthal

    simon_rosenthal

    Joined:
    Jan 3, 2020
    Posts:
    1
    Hey, I'm assuming you already solved the issue, but in case you didn't. I was having the same problem, and I found that another class/file had some errors and was not compiling. By fixing those errors, I was able to have everything compile and I could attach my desired script to my gameobject. Hope that helps!
     
  10. AntonUDT

    AntonUDT

    Joined:
    Jun 5, 2021
    Posts:
    1
    check that the part before the "MonoBehaviour" is the same as your file name.....

    for example, my file name is PlayerController and as i renamed the file from "NewBehaviourScript".
    so it said:

    public class NewBehaviourScript : MonoBehaviour
    instead of
    public class PlayerController : MonoBehaviour

    Thats how i fixed mine...... just remember, its always the little things that dont tend to stand out.

    hopefully this helps someone.
     
    Maxwal likes this.
  11. pci

    pci

    Joined:
    Dec 10, 2012
    Posts:
    3
    That generic dialog is very confusing. I just had the same problem. My file names were all correct no extra spaces. For me it was I had accidentally deleted the last closing bracket in one of my classes and it wasn't the MonoBehavior that I was trying to add. By closing and opening the editor it revealed that I had a compile error -- which caused it to launch in safe mode. Once I fixed the missing bracket it compiled w/o errors and I could add the component to objects without that annoying popup.
     
  12. ElfGambit

    ElfGambit

    Joined:
    Jul 4, 2020
    Posts:
    2
    I traced this down to my version of this problem. This happens to me when I rename a script. After that I cannot attach it to an object to use it. I make a new script of another name with the exact same code and it is fine. Guess the rename function doesn't actually rename everything it needs to. (The files renamed but the class name did not. Class name was the old name.)
     
  13. Kraazon

    Kraazon

    Joined:
    Oct 28, 2020
    Posts:
    1
    I had this problem as well, what i did which worked was delete any spaces in the name, then it worked perfectly
     
  14. spidermark

    spidermark

    Joined:
    Jun 11, 2021
    Posts:
    1
    Worked for me! Thanks!
     
  15. Maxwal

    Maxwal

    Joined:
    Jun 4, 2021
    Posts:
    4
    I fixed this issue by not renaming my script. If you name your script something like XYZ, you need to make sure that your script also has XYZ : MonoBehaviour. If you go in and rename the script file ABC after a while, the script needs to be updated with that name to ABC : MonoBehaviour.
     
  16. stonebreaker707

    stonebreaker707

    Joined:
    Mar 13, 2021
    Posts:
    1
    I had the same problem, but my fix was really really simple, i renamed the script, to the exact same thing, and bam, the problem was gone.
     
  17. edgycoder47

    edgycoder47

    Joined:
    Feb 21, 2021
    Posts:
    2
    you may want to make the name of the script in the editor match with how it's called in the mono behavior loop (don't use the "space" kay when naming your scripts)
     
  18. samuelmorais

    samuelmorais

    Joined:
    Aug 3, 2012
    Posts:
    62
    In may case, the class name was different from the script filename. I just renamed the file to the same name of the class and the error was gone.
     
  19. halfblue3

    halfblue3

    Joined:
    Mar 5, 2019
    Posts:
    11
    Using Add Component works at least. Dragging the script is a no go. What I did is Add Component a script to a GameObject, then, because the code I wanted to add to the GameObject is already written, I copy pasted the code I want to add to the GameObject. It works, but annoying for sure.
     
  20. Meep65

    Meep65

    Joined:
    Jun 8, 2021
    Posts:
    1
    I'm new to unity and this is the third time I've gotten this message since I've started. The solutions to the first two problems was remove the space that was in the script name, The second solution was delete the script, create a new script and add it to your object before you change any part of the code.
     
  21. Byrm07

    Byrm07

    Joined:
    Mar 20, 2020
    Posts:
    1
    Hi guys, in case this would help someone; I was getting this error when I tried to add a script to the object. The reason was uncompleted line of code in another script which was even not attached to anything.
     
  22. Code-Black

    Code-Black

    Joined:
    Mar 28, 2021
    Posts:
    1
    Try creating a new empty object and then adding the code to it. It worked for me.
     
  23. ruohan2

    ruohan2

    Joined:
    Jun 24, 2021
    Posts:
    1
    Encountered the same issue, eventually found out it was because of wrong indentation (thought only Python has that problem)
     
  24. DEBBAJfarouk

    DEBBAJfarouk

    Joined:
    Jun 21, 2021
    Posts:
    6
    If you still get the message (the script doesn't inherit from a native class) I found that when I removed the space from the name of my script the message disapeared .
    At first I couldn't attach the script (Blade Animation) to my gameobject (Blade) and when I changed the name to (BladeAnimation) the problem vanished.
     
  25. toonsend

    toonsend

    Joined:
    Jun 9, 2021
    Posts:
    2
    Make sure your script can compile. For me it was a syntax error in the previous script I was working on, producing this follow on and false error. One of the problems I have experienced if you're working on a Mac is the file system is not always case sensitive, so make sure the name of the file matches in both editor, filesystem, and unity. BUT make sure your program is compiling before adding the script.
     
  26. mKriv123

    mKriv123

    Joined:
    Jun 28, 2021
    Posts:
    1
    I had this problem and I fixed it by making sure that the class name in the script matches the file name in unity. Hopefully this helps.
     
  27. Mlackey

    Mlackey

    Joined:
    Dec 5, 2014
    Posts:
    41
    If using Visual Studio try the following in Unity.

    Edit->Preferences->External Tools

    Make sure Embedded packages, local packages, built-in-packages are checked. Click the "Regenerate project files" button. Then click Refresh or CTRL+R.
     
  28. aigamecreator

    aigamecreator

    Joined:
    Jun 26, 2020
    Posts:
    3
    I found the solution. rename the script file name to class (filename) : monobihaviour like this. it worked for me. file name and class name should be the same without spaces.
     
  29. couchmeka

    couchmeka

    Joined:
    Jul 6, 2021
    Posts:
    1
    This is an old thread but I had this issue when I tried to rename the script more than once. I had to delete the script and add it again then it worked fine.
     
  30. HelloTherePerson

    HelloTherePerson

    Joined:
    Mar 19, 2021
    Posts:
    1
    I fixed it mine by taking out the space in script name. Check if that is the case for u :)
     
  31. karsai106

    karsai106

    Joined:
    Jul 5, 2020
    Posts:
    1
    I just go this happen to me. So in my case it didn't let me put the new script onto the object until I fixed the compiler error in the OTHER script. Maybe it stops compiling if it detects an error so it never compiles the file which you want to drag onto a gameobject. If someone having this issue, just check for compiler errors in the scripts or as other people suggested it check if the script name and the class name matches.
     
  32. wechat_os_Qy045quF3VJ_dMdCg7ddeiOTs

    wechat_os_Qy045quF3VJ_dMdCg7ddeiOTs

    Joined:
    Jun 24, 2019
    Posts:
    1
    I met the same problem, the reasons for this problem is on my side, I renanme my .cs file name, , but I didn't modify files inside the class name.
     
  33. Tehfeelz

    Tehfeelz

    Joined:
    Oct 14, 2020
    Posts:
    1
    THISSSSSS, I was following along with my professor when I was typing code and out of habit with typing I made a class in my folders that was "map generation" and in c# it was "MapGeneration" so they didn't jive, simply removing the space IN the editor fixed all of my problems.
     
  34. scsltd

    scsltd

    Joined:
    Nov 23, 2020
    Posts:
    3
    I agreed the most common reason is difference in file name and header, but I had this occur when I had errors in another file and I was trying to resolve by creating a new file and .breaking code into small chunks.
     
  35. Racketeer04

    Racketeer04

    Joined:
    Jul 29, 2021
    Posts:
    1
    I had the same problem while using unity 2020.4
    My scripts showed no mistake (same as you). What I did was make a change that will not affect the file itself like:

    "public class Script : MonoBeheviour" was changed to "public class Script:MonoBehaviour"

    When I did that the script was recompiled in the unity engine and it worked perfectly
     
  36. sacr0w

    sacr0w

    Joined:
    Aug 1, 2018
    Posts:
    1
    I had this error because I had a SPACE in my script name..
     
  37. ThemulCaelestrasz

    ThemulCaelestrasz

    Joined:
    Sep 20, 2021
    Posts:
    1
    The error (as far as I'm aware) is caused if the name you GAVE the script itself upon creation has any spaces in it.
    This is likely due to c# shortening the script name into a single word (since each unique word is separated unless it's a " " string).

    Use underscores or camelCase instead of spaces when creating a fresh script, else you will get the native class error as your (insert Scriptname Here).cs won't match your ScriptName : MonoBehaviour.
     
  38. HumphreyGames

    HumphreyGames

    Joined:
    May 5, 2020
    Posts:
    40
    I recently bought a PC and it fixed the problem. Maybe it's mac?
     
  39. shaftXD

    shaftXD

    Joined:
    Sep 19, 2021
    Posts:
    1
    I know the issue and solution. If you copy and pasted the code from one script to another, just change the name at the top. it looks like "public class EnemySpawn : MonoBehaviour"
     
  40. benny261103

    benny261103

    Joined:
    Aug 10, 2021
    Posts:
    1
    Hi I just had a similar problem and fixed it:
    1. i named my script Endless Score with a Space
    2. but then i tried it with out a space and it worked

    so remember to write your script name with no spaces in it
    hope this helps people
     
  41. farrellart

    farrellart

    Joined:
    Dec 14, 2019
    Posts:
    16
    There will be an issue with another script somewhere.

    I had this error, I then realised I forgot to amend a public class name from 'trigger' to 'trigger_1' on a script I copied and pasted, when I adjusted that and saved it......all is good again.
     
  42. dchacke

    dchacke

    Joined:
    Apr 6, 2021
    Posts:
    3
    Make sure your script name matches your class name.
     
  43. Banjo_Bill

    Banjo_Bill

    Joined:
    Sep 17, 2018
    Posts:
    2
    I got this error and remembered I changed the name of the script in Unity, but not in Visual Studio. I think the name difference gave me the error. I copied the code (inside the class, not ALL of the code) over to a new C# script (created in Unity and opened in Visual Studio) and it worked fine. Hope this helps.
     
  44. XSchoett

    XSchoett

    Joined:
    Dec 7, 2017
    Posts:
    1
    On Editor 2020.2 : The name of the file and the name of the class must be the same, taking into account the case.
     
  45. lumineyeltd

    lumineyeltd

    Joined:
    May 18, 2019
    Posts:
    1
    Bang on. Such an easy thing to miss.
     
  46. HumphreyGames

    HumphreyGames

    Joined:
    May 5, 2020
    Posts:
    40
    Yeah thanks for this. Looking back on it I'm fairly sure this was the problem.
     
  47. Pawel_Szular_Unity

    Pawel_Szular_Unity

    Joined:
    Apr 10, 2022
    Posts:
    1
    Thanks for the help :):):)
     
  48. paymanbehnami

    paymanbehnami

    Joined:
    Aug 21, 2018
    Posts:
    5
    Hello my friend, the error is because of the namespace
     
  49. I_am_bored203

    I_am_bored203

    Joined:
    Feb 9, 2023
    Posts:
    1
    i had this error happen to me but it was on one script
     
  50. Vladizem

    Vladizem

    Joined:
    Nov 18, 2023
    Posts:
    1
    I fixed that Problem,
    click on Player, then in the Inspector(on right site) Add component, new script, und create now script, to evade this bug. this bug don`t come out.