Search Unity

No MonoBehaviour scripts in this file?? {Answered}

Discussion in 'Scripting' started by NintendoMaster00, Mar 28, 2015.

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

    TYJIE123

    Joined:
    Nov 18, 2016
    Posts:
    2
    try to check your script name
    make sure it do not have space between the name
    and the name same with this line:
    public class yourscriptname : MonoBehaviour
    this is the solution for me...
     
  2. IrvEru

    IrvEru

    Joined:
    Nov 16, 2018
    Posts:
    1
    Check the name.
     

    Attached Files:

  3. bencarley

    bencarley

    Joined:
    Dec 18, 2018
    Posts:
    1
    Hi, disclaimer before we go on, I can't code to save my life. Just started, I thought unity gave me headaches. Anyway, I'm trying to get a functional cursor lock because someone had the genius idea to remove it during the time I haven't bothered to update so I'm kind of screwed. I'm actually getting decent at unity now but the cursor is a massive problem. At the moment I am trying to use the script provided by unity as an example (trying and failing)and fixing what little I understand. Once I got all but one errors away, same script name and monobehaviour name(?) it claimed it was not under a monobehavior. I changed the name and monobehaviour name to A to be sure, no cheese.

    using UnityEngine;

    using System;

    public class A : MonoBehaviour


    {

    CursorLockMode wantedMode; ----It wants another identifier here after the bracket for some reason----

    // Apply requested cursor state


    void SetCursorState(A)


    {

    Cursor.lockState = wantedMode;

    // Hide cursor when locking


    Cursor.visible = (CursorLockMode.Locked != wantedMode);

    }

    void OnGUI()


    {

    GUILayout.BeginVertical();

    // Release cursor on escape keypress


    if (Input.GetKeyDown(KeyCode.Escape))


    Cursor.lockState = wantedMode = CursorLockMode.None;

    switch (Cursor.lockState)


    {

    case CursorLockMode.None:

    GUILayout.Label("Cursor is normal");

    if (GUILayout.Button("Lock cursor"))


    wantedMode = CursorLockMode.Locked;

    if (GUILayout.Button("Confine cursor"))


    wantedMode = CursorLockMode.Confined;

    break;

    case CursorLockMode.Confined:

    GUILayout.Label("Cursor is confined");

    if (GUILayout.Button("Lock cursor"))


    wantedMode = CursorLockMode.Locked;

    if (GUILayout.Button("Release cursor"))


    wantedMode = CursorLockMode.None;

    break;

    case CursorLockMode.Locked:

    GUILayout.Label("Cursor is locked");

    if (GUILayout.Button("Unlock cursor"))


    wantedMode = CursorLockMode.None;

    if (GUILayout.Button("Confine cursor"))


    wantedMode = CursorLockMode.Confined;

    break;


    }

    GUILayout.EndVertical();

    SetCursorState();

    }

    private void SetCursorState()


    {

    throw new NotImplementedException();


    }

    }

    Any help would be much appreciated. I am currently at the mercy of winging it. A lot harder to do with C# than with Unity engine. It's basically the unity version with the name changed to A. By basically I mean it's identical bar the fact I changed the name to A. I have no idea what I am doing. Fml.
     
  4. Please open a new thread for your problem, this thread has nothing to do with yours.
    Also please post your code in code tags. You can find the how-to here.
     
  5. StarrFighter05

    StarrFighter05

    Joined:
    Jun 6, 2018
    Posts:
    1
    I have this problem too. It is saying no monobehaviour scripts in the file, or their names don't match the file name.
    It gave me this error on all the scripts I have. I also made a game that when I ran it in play mode it worked fine but when I build it the whole game was broken. Then I made a new project and made a new script and it instantly had the problem. I try adding it to a empty game object but it just says cant add script behaviour TMP_SelectionCaret. The script needs to derive from MonoBehaviour.
     
  6. thway

    thway

    Joined:
    Feb 5, 2019
    Posts:
    2
    who do you go to your docs
     
  7. thway

    thway

    Joined:
    Feb 5, 2019
    Posts:
    2
    how do you go to your docs
     
  8. Thefudgeway

    Thefudgeway

    Joined:
    Mar 8, 2019
    Posts:
    1
    For the people that still encounter this issue:

    I had the same problem, even when i switched projects, cleaned projects, reinstalled software or made sure that all the names were correct in the script.

    Solution:
    In my case i was using some assets from a imported package. In the new assets there was a script that contained an error/wasnt working properly. After erasing this script everything worked again. So make sure to check your errors from an imported package to find that S***ty script that is wasting your time.

    hope it helps for some of the people!
     
  9. oweniaat

    oweniaat

    Joined:
    May 12, 2019
    Posts:
    2
    well i did that no change. help
     
  10. oweniaat

    oweniaat

    Joined:
    May 12, 2019
    Posts:
    2
    This did nothing
     
  11. If you want help, post: your entire script in code tags(!). Screenshot of your editor when your selection is on the same file and the file name and the inspector is visible and the exact error or errors (if you have more, it's important!).
     
  12. wechat_os_Qy03mTPWKCCYcR3Izo_lz8zfg

    wechat_os_Qy03mTPWKCCYcR3Izo_lz8zfg

    Joined:
    May 19, 2019
    Posts:
    1
    i met the same problem,i changed my file name and class name but it still didn't work.but i figured out that i had new a 3D project but import 2D Assets..... then i new another 2D project and copy the code into it then it works......
     
  13. CasperCoder

    CasperCoder

    Joined:
    Jul 29, 2012
    Posts:
    13
    I've had this happen twice now with previously working classes and files. Re-importing didn't seem to make a difference, nor closing Unity and deleting the script assemblies in the Library directory. Had to recreate new files both times and then copy back over the GUID in the meta file to re-establish any links to prefabs.

    Unity must be caching something... somewhere. It's very mysterious.
     
  14. c4cqi

    c4cqi

    Joined:
    Oct 11, 2019
    Posts:
    2
    Thank goodness. This worked for me. When I'd started to create a new script I accidentally named it '....-.cs'. I then renamed it. The fix was to delete it, recreate it, re-import all. Many thanks!
     
  15. ruban211097

    ruban211097

    Joined:
    Jan 25, 2020
    Posts:
    2
    If you have problems with Package Cache - Go to "Help" tab, and click "Reset packages to defaults"
     
    agupta123, pizzaknife and deusebio like this.
  16. ZachYarbrough

    ZachYarbrough

    Joined:
    Oct 4, 2019
    Posts:
    1
    If you get this error after changing the name of a script then make sure you do not call the changed script in any other scripts. If you do you need to change the name inside of that script too.
     
  17. pizzaknife

    pizzaknife

    Joined:
    Oct 1, 2020
    Posts:
    2
    This worked on a fresh install of 2020.1.8f1 Personal
     
  18. IRSMAKER

    IRSMAKER

    Joined:
    May 3, 2020
    Posts:
    2
    I know this is a late reply but I still got this issue in 2020.1.2f1 version.

    1) My All Scripts Got the same error "No Monobehaviour Scripts in the file...."
    2) I Created A New Script. Copied the Script Content and Matched the Names But Still Same Error.
    3) I reimported All The Assets but that doesn't worked.
    4) Restarted the Unity and VS many times but Not worked.
    5) Set the "Edit/Preferences/External tools" to VS and opened project in VS again but doesn't worked.
    6) I have Unity buildtools installed for VS.

    At last I managed to Solve the issue by.

    1) Open your project in unity and wait to load completly.
    2) Goto Console ( At the bottom left side ) and Click on "Clear" Button.
    3) Once you clicked the Clear button Check for the remaining errors.
    4) Solve all the errors one by one.
    5) Wait for unity to reimport all the prefabs.
    6) And Done.

    I hope It will Help.

    Actually last night I saved my project with an error which i didn't know that time. I had an reference error in my one script. The next day when I opened my project again I got All my scripts with the monobehaviour error. I am so happy that I solved it at last lol.
     
    Last edited: Dec 15, 2020
  19. sielay

    sielay

    Joined:
    Jan 11, 2021
    Posts:
    1
  20. maorioriori

    maorioriori

    Joined:
    Jan 15, 2021
    Posts:
    1
    I got the same issue and lots of the errors are on a dll that exists more than once, I deleted all the objects that I dragged more than once into the scene and it is still complaining about the exact same issue.
    The error messages are definitely not clear enough to understand where the problem is and feels like unity is complaining about the wrong things.
     
  21. dawidziemann25

    dawidziemann25

    Joined:
    Mar 9, 2021
    Posts:
    1
    hello i just got unity and i watched a video how too make a game everything when ok but then i tried to open the c# script then i would press the c# script it would just say were do you what to put his file, and i put it in unity hub because i thought it would work but it did not no it goes to unity hub
    please help :(
     
  22. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Please don't post to 6-year-old unrelated threads. It is against forum rules.

    Instead, create your own post, it's FREE!

    When you post, here is how to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
Thread Status:
Not open for further replies.