Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

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

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

  1. joshuahalls13

    joshuahalls13

    Joined:
    May 5, 2020
    Posts:
    17
    Hello!

    I know that is problem is on many other threads but i could not find an answer.

    Every time i make a new script and attach it to the players gameobject, it comes up with the error "The script don't inherit a native class that can manage a script." It even happens when i have just created the script.

    I have tried:

    Starting a new project
    Deleting the scripts and re-writing them

    Nothing seems to work :(

    Any help?

    Thanks :)
     
    xluo3 likes this.
  2. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    600
    Hey there, maybe post a code example ?
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,699
    public class MyClass : MonoBehaviour
     
    Shaw358 and stevenleo06 like this.
  4. joshuahalls13

    joshuahalls13

    Joined:
    May 5, 2020
    Posts:
    17
    Ok here is my code:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using TMPro;
    7.  
    8. namespace Invector.vCharacterController
    9. {
    10.     public class pickUpSword : MonoBehaviour
    11.     {
    12.         #region Vairables
    13.  
    14.         [Header("Variables")]
    15.         public KeyCode pickupInputCon = KeyCode.JoystickButton5;
    16.         public KeyCode pickUpInputKey = KeyCode.E;
    17.         public KeyCode unSheathInputCon = KeyCode.JoystickButton3;
    18.         public KeyCode unSheathInputKey = KeyCode.R;
    19.         public KeyCode sheathInputCon = KeyCode.JoystickButton4;
    20.         public KeyCode sheathInputKey = KeyCode.T;
    21.  
    22.         private Animator anim;
    23.         private vThirdPersonController cc;
    24.  
    25.         public GameObject swordInHand;
    26.         public GameObject swordToPickUp;
    27.         public GameObject swordOnBack;
    28.  
    29.         public bool hasSomethingInHand;
    30.         public bool hasUnSheathed;
    31.         public bool hasSomethingOnBack;
    32.         public bool isSwordInHand;
    33.         public bool canPickUp;
    34.  
    35.         private GameObject thisGameObject;
    36.         public float distance;
    37.  
    38.         #endregion
    39.  
    40.         void Awake()
    41.         {
    42.             anim = GetComponent<Animator>();
    43.             cc = GetComponent<vThirdPersonController>();
    44.             thisGameObject = this.gameObject;
    45.         }
    46.  
    47.         void Update()
    48.         {
    49.             HandleInputs();
    50.         }
    51.  
    52.         void HandleInputs()
    53.         {
    54.             HandleInput();
    55.             DistanceHandler();
    56.         }
    57.  
    58.         void DistanceHandler()
    59.         {
    60.             distance = Vector3.Distance(thisGameObject.transform.position, swordToPickUp.transform.position);
    61.  
    62.             if (distance <= 0.50)
    63.             {
    64.                 canPickUp = true;
    65.             }
    66.             else
    67.             {
    68.                 canPickUp = false;
    69.             }
    70.         }
    71.  
    72.         void HandleInput()
    73.         {
    74.             if (Input.GetKeyDown(pickupInputCon) && hasSomethingInHand == false && hasSomethingOnBack == false || Input.GetKeyDown(pickUpInputKey) && hasSomethingInHand == false && hasSomethingOnBack == false && canPickUp == true)
    75.             {
    76.                 anim.SetTrigger("pickUp");
    77.             }
    78.  
    79.             if (Input.GetKeyDown(unSheathInputKey) && hasSomethingOnBack == true && hasSomethingInHand == false || Input.GetKeyDown(unSheathInputCon) && hasSomethingOnBack == true && hasSomethingInHand == false)
    80.             {
    81.                 anim.SetTrigger("unSheath");
    82.             }
    83.  
    84.             if (Input.GetKeyDown(sheathInputKey) && hasSomethingOnBack == false && hasSomethingInHand == true || Input.GetKeyDown(sheathInputCon) && hasSomethingOnBack == false && hasSomethingInHand == true)
    85.             {
    86.                 anim.SetTrigger("sheath");
    87.             }
    88.         }
    89.  
    90.         //Turn on/off sword in players hand
    91.         void TurnOnSwordInHand()
    92.         {
    93.             swordInHand.SetActive(true);
    94.             hasSomethingInHand = true;
    95.             hasSomethingOnBack = false;
    96.             cc.isStrafing = true;
    97.             isSwordInHand = true;
    98.         }
    99.  
    100.         void TurnOffSwordInHand()
    101.         {
    102.             swordInHand.SetActive(false);
    103.             hasSomethingInHand = false;
    104.  
    105.             cc.isStrafing = false;
    106.             isSwordInHand = false;
    107.         }
    108.  
    109.         void TurnOffSwordToPickUp()
    110.         {
    111.             swordToPickUp.SetActive(false);
    112.             cc.isStrafing = true;
    113.             isSwordInHand = true;
    114.  
    115.             if (swordToPickUp == null)
    116.             {
    117.                 Destroy(swordToPickUp);
    118.             }
    119.         }
    120.  
    121.         //Turn on/off swordOnBack
    122.         void TurnOnSwordOnBack()
    123.         {
    124.             swordOnBack.SetActive(true);
    125.             hasSomethingOnBack = true;
    126.             hasUnSheathed = false;
    127.             cc.isStrafing = false;
    128.             isSwordInHand = false;
    129.         }
    130.  
    131.         void TurnOffSwordOnBack()
    132.         {
    133.             swordOnBack.SetActive(false);
    134.             hasUnSheathed = true;
    135.             cc.isStrafing = true;
    136.             isSwordInHand = true;
    137.         }
    138.     }
    139. }
     
  5. joshuahalls13

    joshuahalls13

    Joined:
    May 5, 2020
    Posts:
    17
    Hi yeah I made sure that I am inheriting from MonoBehaviour. Thanks for the suggestion though :)
     
    starmue2 likes this.
  6. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    600
    how is the file with the script called ? should be precisely "pickUpSword.cs".
     
  7. joshuahalls13

    joshuahalls13

    Joined:
    May 5, 2020
    Posts:
    17
    Yes the name of the script is exactly the same as the visual studio name. I even tried copy and pasting it just to make sure
     
  8. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,699
    Try creating a brand new Unity project and see if you get the same problem there.
     
  9. joshuahalls13

    joshuahalls13

    Joined:
    May 5, 2020
    Posts:
    17
    Hi so I tried starting a new project and everything seemed to work fine but then it happened again after about 5-10 minutes. Should I change to another unity version? I am currently on the latest 2020 version
     
    NatalieWeishuhn likes this.
  10. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    600
    thats odd, try removing the namespace
     
  11. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,699
    Assuming you were doing stuff in the editor during that 5-10 minutes, then it's probably related to something you did in that time. Try to remember everything you did from the last time you know your scripts worked until the first time you know they didn't. See if you can undo those things and make them work again. If you can't get your scripts to work again, start another new project and do stuff again, but check for this error as often as you can so that you know exactly when things go wrong and can figure out what you did right before that.
     
  12. joshuahalls13

    joshuahalls13

    Joined:
    May 5, 2020
    Posts:
    17
    l will try removing the namespace and creating a new project. If that doesnt work i will try and delete unity and restart. Oh yeah btw during those 5-10 mins i was just writing the script
     
  13. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,699
    So after you wrote that one particular script, all scripts in your project stopped working? If you delete that script from your project (you can save a copy elsewhere on your computer if you want), then do other scripts work again?
     
  14. joshuahalls13

    joshuahalls13

    Joined:
    May 5, 2020
    Posts:
    17
    Yes, all scripts stop working. When i try deleting it nothing happens
     
  15. joshuahalls13

    joshuahalls13

    Joined:
    May 5, 2020
    Posts:
    17
    I think its a bug but maybe not
     
  16. zoeyducoeur

    zoeyducoeur

    Joined:
    Dec 25, 2020
    Posts:
    1
    I went back into my ctrl+z and found I accidentally moved the MonoBehaviour script from it's home. Not saying that'll work for you but it fixed my "The script don't inherit a native class that can manage a script" problem
     
  17. Kinnith7

    Kinnith7

    Joined:
    Jul 4, 2017
    Posts:
    31
    If you are still having a problem, I imagine not, it's been six days. But I just had the same thing happen and it was because I had an extra space in the .cs script name that wasn't in the actual code.
     
  18. DevboxBrasil

    DevboxBrasil

    Joined:
    Dec 26, 2020
    Posts:
    1
    Guys, it's definitly a bug from Unity 2020. I'm using 2020.2.1f1 and I'm getting this all the time.
    I have fixed this one time when I noticed the script name was different from the class name.
    Right now I was trying to drop a brand new script to an existing game object in my scene and I was getting this error.

    Later I found out that one of my other scripts was having a compile error, I just fixed this and I could drop the script to meu object. This is like... WTF? Non-sense error...

    Unity definitly needs to do something about this...
     
  19. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    468
    One trick!

    If you try to add a script while the game is Play, you get this (badly worded) message!
     
    timsamoff, matt_maher and Nithium like this.
  20. Reversecard456

    Reversecard456

    Joined:
    Jan 19, 2021
    Posts:
    2
    hello, it's my first day in unity and I am on my first project, I am having the same problem, so I tried making a new script without writing anything in it I dragged it to the player and the same message came again anyway does anyone know how to fix it in my case? thank you
     
    Last edited: Jan 19, 2021
  21. nathonion

    nathonion

    Joined:
    May 12, 2016
    Posts:
    2
    This problem was happening to me too, but I was able to fix it by simply making sure the rest of my code didn't have any errors (as a temporary fix, I just commented out the lines that were giving the errors), and I was then able to add new scripts to objects without the error. Not sure if it'll work for anyone else here, but I figured it was worth putting out there.

    Edit: btw, I'm using Mirror, so I don't know if that had anything to do with the error...
     
  22. Nithium

    Nithium

    Joined:
    May 18, 2016
    Posts:
    7
    For GOD SAKE i cant belive this was the cause of my problem lol
     
  23. Reversecard456

    Reversecard456

    Joined:
    Jan 19, 2021
    Posts:
    2
    can anyone help me
     
  24. TechS390

    TechS390

    Joined:
    Dec 20, 2020
    Posts:
    1
    I'm fairly new but Something within the script must refer to what it affects within Unity. The Public Classes Mono Behavior, I'm new-ish though, better to use my suggestion as a starting point rather than a definitive answer.
     
  25. lionreich

    lionreich

    Joined:
    Oct 7, 2020
    Posts:
    1
    OK! I managed to fix it, but I'm not entirely sure how. So I deleted ALL the scripts I had been working on and added to the scene. I noticed that when I started getting this error Unity stopped updating the scripts associated with the project. So of the three scripts I deleted one was the offender, and I took the code from the one I knew that worked before this happened and pasted in a fresh file, and it applied! It played! Everything worked. It seems as if one faulty script in your assets can prevent all other scripts in your scene from progressing. It doesn't even have to be the one you're working on.
     
  26. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    600
    unity needs to be able to compile indeed, after a new script has been compiled once unity can use its cache in case there are errors preventing it from compiling, but until that first time compiling you're stuck with whatever version you had before.
     
  27. owenreinfordor

    owenreinfordor

    Joined:
    Jan 5, 2021
    Posts:
    1
    I had this issue aswell and upon double checking the code I noticed that the name of the script in the project window had a space in it wheras the name after public class did not. Check if your script name has a space and if it does remove it. Hopefully this helps.
     
  28. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    729
    I'm having this problem while upgrading to Unity 2020.2.2. The project was working with 2020.2.1. What else did you guys try?

    Edit: I fixed it. Just fix the compile errors from other parts.
     
    Last edited: Jan 26, 2021
    Serg1777 likes this.
  29. Harsso

    Harsso

    Joined:
    Jan 26, 2021
    Posts:
    1
    I had the same problem. I solved it going to "Package Manager" / In Project, and then removing "Package Manager UI" or something like that
     
  30. joshuahalls13

    joshuahalls13

    Joined:
    May 5, 2020
    Posts:
    17
    Hi i haven't been on here a while as i took a break over christmas. I literally had to delete unity and re install it. Even then i get the message but only one or twice and i just have to click cancel and retry. Thanks for all the help guys!
     
  31. cremedemaron

    cremedemaron

    Joined:
    Jan 31, 2021
    Posts:
    1
    hello!
    I had a same erreor message:
    The script don't inherit a native class that can manage a script

    I resolve this probleme dowload some a laster version of visual studio.
    I don't know if you us visual studio for coding your script or if your verison
    is up to date.
    but this working for me.


     
  32. plashivranov

    plashivranov

    Joined:
    Feb 6, 2016
    Posts:
    1
    Hey, guys. What fixed it for me - I quit Unity Editor, then deleted all the *.metadata files from the /Scripts folder and restarted Unity. The editor created new *.metadata files. I had to reattach certain components to the scripts, but all works fine now.
     
  33. Just_Jo

    Just_Jo

    Joined:
    Feb 18, 2020
    Posts:
    1
    Hey, I don't know much about Unity, but I only saw this error when I had a number in the name of the script. Now it works for me ... *in my case it was "2DCharacterController"
     
  34. definitivebanana

    definitivebanana

    Joined:
    Feb 6, 2021
    Posts:
    1
    you have to delete the espaces or the numbers of the script name
     
  35. yyylny

    yyylny

    Joined:
    Sep 19, 2015
    Posts:
    53
    Happened to me today because I created a new script with a space in its name, i.e. "My Script", so I renamed it to "MyScript" and that fixed the problem. Unity shouldn't allow naming scripts with illegal class name characters like a space and that illegible error message makes it even more confusing.
     
    Pratzz_d and MikkoHaavisto like this.
  36. Sukruth_Ramesh

    Sukruth_Ramesh

    Joined:
    Jan 29, 2021
    Posts:
    1
    Even I had the same problem. I realised that when there is an error in the script then the message comes. I corrected my error and the message no longer came.
     
  37. marshmatter

    marshmatter

    Joined:
    Oct 2, 2013
    Posts:
    44
    May not be 100% same issue as what's discussed above, but I hit this error today after a branch switch.

    There was a naming mismatch between the .cs file name and the MonoBehaviour class name, e.g.
    SearchToolInstaller
    vs.
    SearchToolinstaller
    . Hard to spot and not sure how the branch I switched to got out of whack, but fixed it for me once corrected.
     
  38. spartang30

    spartang30

    Joined:
    Feb 11, 2021
    Posts:
    1
    I fix this problem by changing the name of my script and copy-paste the exact same name in public class ClassName : MonoBehaviour

    Edit: My script name is PlayerBehavior but in the class was PlayerBehaviour and there was the problem, you need to be sure the first time you create a script and name it, because if you rename later wont update your class name, you need to do it manually
     
  39. GermanBenavides

    GermanBenavides

    Joined:
    Nov 15, 2016
    Posts:
    1
    Efectivamente se soluciona el problema al cambiar el nombre public class name
     
  40. MykeBlack

    MykeBlack

    Joined:
    Apr 16, 2020
    Posts:
    11
    I found this same error today. The problem was because I pasted some code in and it changed the class name. So if you find this, make sure the name of the class exactly matches the name of the .cs file. Even make sure captials are the same too.
     
  41. mathias0411

    mathias0411

    Joined:
    Sep 19, 2020
    Posts:
    1
    make sure that the name of the script is right!
     
  42. Nicklefritz

    Nicklefritz

    Joined:
    Feb 21, 2021
    Posts:
    1
    Hey, Josh, I just had the same problem in my unity project (found my issue, but decided to check on yours.) I brought your code into my project just to see if I could get it to work; now, I'm a beginner, so there's a lot of your code I don't yet understand, however, using Visual Studio 2017, I was getting an error on lines 23 and 43 (these line number are taking in consideration of the empty line 1 you have in your above code... other wise, they would be lines 22 and 42).

    23. private vThirdPersonController cc;

    43. cc = GetComponent<vThirdPersonController>();

    In attempting to debug your code, I commented out ( " // " ) these two lines; of course doing so generated errors on lines that included:

    cc.isStrafing = false;

    (lines: 96, 105, 112, 127, and 135). After commented out these lines of code as well, I was able to run your script (or at least attach it successfully to a game object) in unity. I don't know if I got these errors on lines 23 and 43 because the script is inherently flawed, or if its simply because I'm not using your project or your other components; however, it might be worth investigating these pieces of your code. Perhaps you'll find the error lying somewhere in there!

    Let me know if this lead you to your solution!

    Sincerely,
    Nicklefritz



     
  43. ilyacherney

    ilyacherney

    Joined:
    Oct 14, 2020
    Posts:
    1
    I had the same problem guys and now it's gone. Try Help - Reset Packages to defaults
     
    Zongoland likes this.
  44. unity_x6Zl-zvtB8Mp_g

    unity_x6Zl-zvtB8Mp_g

    Joined:
    Feb 24, 2021
    Posts:
    1
    Make sure the name of the script is the same as the name of the class u are using.
     
    onoclushadowruntron likes this.
  45. Avingo255

    Avingo255

    Joined:
    Nov 27, 2020
    Posts:
    1
    You have to change the class name in ClassName : MonoBehaviour to the exact name of your file, otherwise it won't work
     
    Redmutant9 and karimhesham00 like this.
  46. Waystep

    Waystep

    Joined:
    Feb 25, 2021
    Posts:
    1
    It workkk Thanks
     
  47. CynderR_nAsh

    CynderR_nAsh

    Joined:
    Jul 31, 2020
    Posts:
    1
    For me it was happening because i was trying to add a script to an object while i had an error in the console. Commenting out the code that caused the error allowed me to attach the script to the objext.
     
  48. guruleroger

    guruleroger

    Joined:
    Mar 11, 2014
    Posts:
    4
    I was up ALL night trying to figure this out. I was doing the Ruby 2D tutorial and then created and named the first script 'Ruby Controller'. I spent forever trying to figure out why I was getting that error and finally gave up for the night. You're tip saved me! I changed the name of my script to 'RubyController' and it fixed it. I didn't realize that the name of the script in the Project window would be linked to the 'public class' declaration in the script itself (which I believe cannot have spaces).
    :rolleyes::rolleyes::rolleyes: thanks again!
     
    onoclushadowruntron likes this.
  49. Smoole

    Smoole

    Joined:
    Nov 3, 2017
    Posts:
    6
    Its a bug of .meta files, if you don't have any errors (like the ones above of spaces in the name) and you think everything is alright, just delete the .meta files and let unity re-create them automatically, it will be fixed :)
     
  50. hjtforrest

    hjtforrest

    Joined:
    Mar 9, 2021
    Posts:
    1
    you can try to upgrade the package Manage UI (Window > Package Manager> Packages: Unity Registry> package Manage UI) upgrade
     
unityunity