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:
    21
    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:
    21
    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.