Search Unity

[BUG] "No MonoBehaviour in the file" when using the new Assembly Definition "Version Defines"

Discussion in 'Scripting' started by brunocoimbra, May 31, 2019.

  1. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
    Hello Unity Team!

    When using the new "Version Defines" on Assembly Definition files, any MonoBehaviour put inside a namespace encapsulated inside a compiler conditional will fail to be recognized by Unity as a valid MonoBehaviour.

    To reproduce it:

    1. Create an Assembly Definition file.
    2. In Version Defines, add one item with a symbol that will compile.
    3. Create a MonoBehaviour inside a namespace encapsulated by that symbol you just created.
    4 Try to add that newly created MonoBehaviour into a GameObject by drag-n-drop, Unity will warn that there is no MonoBehaviour in the file.

    I can still add the component via scripting (gameObject.AddComponent<MyComponentInsideANamespace>()).
    Also, if I remove the namespace Unity will allow you to add the component through drag-n-drop just fine.

    Already made a bug report, case #1159277

    Screenshot_1.png Screenshot_3.png Screenshot_4.png Screenshot_5.png Screenshot_6.png
     
  2. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
    For those who may cross against that same issue, here is a temporary solution until Unity fix that:
    Code (CSharp):
    1. #if MY_DEFINE
    2. using UnityEngine;
    3.  
    4. [ExecuteAlways, AddComponentMenu("Scripts/My Behaviour With That Issue")]
    5. internal class AddComponentMyBehaviourWithThatIssue : MonoBehaviour
    6. {
    7.     private void Reset()
    8.     {
    9.         gameObject.AddComponent<MyBehaviourWithThatIssue>();
    10.     }
    11.  
    12.     private void Update()
    13.     {
    14.         DestroyImmediate(this);
    15.     }
    16. }
    17. #endif
    18.  
    Well, not the perfect solution, but I can live with that for now.
     
  3. paolo_m_ts

    paolo_m_ts

    Joined:
    Oct 22, 2018
    Posts:
    3
    @brunocoimbra did Unity get back to you about the bug? I can't find any info online. I'm having the same issue in Unity 2019.2.10.
     
  4. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
    Actually they had, here is their reply:

    "Hi,

    We have investigated this case thoroughly and have fixed it in 2019.3.
    In the interests of protecting the stability and features of Unity for users, we will not backport the fix since it was a quite complex change.

    To handle this, you could move the namespace definition outside of the #define preprocessor scope.

    Harald."
     
    paolo_m_ts likes this.