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

No Monobehaviour scripts in the file

Discussion in 'Scripting' started by Flavelius, Aug 11, 2018.

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

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    926
    Hi,
    i have this well known issue, that unity seemingly does not detect a monobehaviour in my script.
    -The file name is the same as the class name
    -It inherits from another class that inherits from MonoBehaviour. This class also has no typo, but is also not recognized as MonoBehaviour
    -Both are inside the same namespace
    Interestingly the script is correctly assigned on prefabs (the inspector's 'Script' field links to the correct script file, it's not missing), only that it displays this error message (please fix the script etc..) instead of showing fields.

    I already reimported, deleted Library, reopened unity; nothing changed.
    Then i switched the inspector into debug mode and saw that the namespace field there is empty. As it is readonly i cannot change it, but i suspect that this is the cause of my problem.

    How can i fix it?

    Edit: 2018.2.2f1, both files are also in different folders
     
    Last edited: Aug 11, 2018
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,574
    Test from very basics.
    Try first, if clean base class works with Game Object.
    Then secondly, when inhering base class from second class, try again repeat attaching to GameObject process.
    Keep to very minimum class, without properties first. Then try add and see, if or where the issue may ocure.
     
  3. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    926
    i already did this; a new Monobehaviour doesn't have that problem. The class itself actually currently does not even have fields or properties (nor does the base class).
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Post the actual code so we can have a look.
     
  5. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
  6. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    926
    i doubt that will help, but:
    Code (CSharp):
    1. filename: BaseObject.cs
    2. using UnityEngine;
    3. namespace Engine
    4. {
    5.     public class BaseObject: MonoBehaviour
    6.     {
    7.        //[SerializeField] int resourceID; //this field was already commented out before
    8.  
    9.        public BaseObject() { } //i know this doesn't make sense, it's there because the files are autogenerated
    10.     }
    11. }
    12.  
    13. filename: ObjectPackage.cs
    14. namespace Engine
    15. {
    16.     public class ObjectPackage: BaseObject
    17.     {
    18.      
    19.     }
    20. }
    what i also did though was add AssemblyDefinitions at some point, but deleted them later, moved the scripts between folders and changed the namespace a few times. Other scripts don't have that problem though.
     
  7. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    926
    When i remove the namespace it is recognized correctly. So i guess this really is related to what i mentioned in the entry post
     
  8. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I can't reproduce the problem and the only difference I can see is that my classes are called Test and TestDerived. Otherwise, exactly identical to what you have posted.
     
  9. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    926
    I also can't reproduce it with a new project. Maybe it's related to adding, removing the assemblydefinitions and moving things around; i guess that broke something somewhere deeper in the editor.
    i can keep those two classes outside the namespace though, i'll work with that.
    Thanks for the help!
     
  10. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    926
    i just discovered that this happened to some other random classes aswell, where i cannot easily remove the namespace. Is there any way i can force a different kind of reimport of the scripts (the usual 'Reimport' doesn't change anything), so the namespaces are attempted to be recognized again?
     
  11. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,574
    In Project folder via Unity, you have also Reimport All and Refresh, if basic Refresh not working. Try these.
     
  12. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    926
    Update: i just ran Resharper cleanup over all the files and that fixed it. I guess the script file parser had some problems with certain layouts.
     
  13. andrew-lukasik

    andrew-lukasik

    Joined:
    Jan 31, 2013
    Posts:
    239
    Thanks for detailing your case @Flavelius . I was very puzzled myself seeing this in 2020.1.0a7 and my case matches what you described - problem arising shortly after when assembly definition files were removed. But now I'm thinking what to do since I don't have that Resharper thing.
    Removing namespaces certainly works, but is hardly a solution.
     
  14. andrew-lukasik

    andrew-lukasik

    Joined:
    Jan 31, 2013
    Posts:
    239
    I tested this issue some more and managed to pinpoint rather oddly specific code that is causing this in my case:
    Code (CSharp):
    1. void LoremIpsum ()
    2. {
    3.     #if LOREM_IPSUM
    4.         string str = $"Lorem Ipsum {GetInstanceID()}";
    5.     #endif
    6. }
    Apparently it requires both a preprocessor block to be there and a method call nested inside interpolated string.

    PLOT TWIST: Reported as bug and was informed this is a known issue
    script-is-not-recognized-as-a-monobehaviour-if-it-contains-string-concatenations-in-a-lambda-inside-number-if-unity-editor-block
     
    Last edited: Oct 9, 2019
  15. grimjim

    grimjim

    Joined:
    Jan 18, 2014
    Posts:
    17
    I had the same problem and the solution for me was that the class name was different to the file name for example file: TestClass.cs and code: public class Testclass: TestBaseClass
     
Thread Status:
Not open for further replies.