Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

C#6 using static problem

Discussion in 'Experimental Scripting Previews' started by dungeonkim, Apr 7, 2017.

  1. dungeonkim

    dungeonkim

    Joined:
    Mar 30, 2017
    Posts:
    1
    Hi.
    I found a editor bug that I could not add component when using using static with a namespace definition.

    Code (CSharp):
    1. using static System.Math;
    2.  
    3. namespace Test
    4. {
    5.     public class A : MonoBehaviour {
    6.  
    7.         :
    8.     }
    9. }
    When I write the above code and try to add component to the game object, I get a dialog box saying that the script class can not be found.
     
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    Which version of Unity are you using in this case?
     
  3. testqaz

    testqaz

    Joined:
    May 21, 2017
    Posts:
    5
    Same here, I use version 5.6.1f1, with the CSharp60Support package.
    Everythings else work like a charm except this.
    Any help would be appreciated.
    Thanks.
     
  4. joncham

    joncham

    Unity Technologies

    Joined:
    Dec 1, 2011
    Posts:
    276
    Hello,

    We currently parse the source files to determine the name of the MonoBehavour contained within it. The parser does not understand some new C# features currently.

    This will be supported at some point in the future when we fully support .NET 4.6 and C# 6.

    Thanks,
    Jonathan
     
  5. SugoiDev

    SugoiDev

    Joined:
    Mar 27, 2013
    Posts:
    395
    This is also the case with the current experimental .net 4.6 on Unity 2017.1.0
    Adding any using static directive before a MonoBehavior definition will cause Unity to no longer see the class as a MonoBehavior.
     
  6. Deleted User

    Deleted User

    Guest

    Same here,
    2017.3.1p3
     
  7. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    Can you submit a bug report for this issue? We would like to correct it.
     
  8. Deleted User

    Deleted User

    Guest

    @JoshPeterson I don't know if I find the time for this to isolate, but I think what @dungeonkim wrote is already enough in an empty project with experimental 4.6 enabled:


    1. Code (CSharp):
      1. using static System.Math;
      2. namespace Test
      3. {
      4.     public class A : MonoBehaviour {
      5.         :
      6.     }
      7. }
     
  9. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    I've just tried this in 2018.1, and it looks to be working correctly. Can you try it in that version?
     
  10. MoncGreen

    MoncGreen

    Joined:
    Sep 14, 2017
    Posts:
    3
    I'm having this problem in 2018.1 beta 13.

    Something like the below will work.
    Code (CSharp):
    1. using UnityEngine;
    2. using static UnityEngine.Mathf;
    3.  
    4. public class Tester : MonoBehaviour {
    5.    
    6. }
    7.  
    However the below does not.
    Basically namespaced MonoBehaviours are broken

    Code (CSharp):
    1. using UnityEngine;
    2. using static UnityEngine.Mathf;
    3.  
    4. namespace DeathByNamespace
    5. {
    6.     public class Tester : MonoBehaviour
    7.     {
    8.        
    9.     }
    10. }
    11.  
     
    pragmascript likes this.
  11. Deleted User

    Deleted User

    Guest

  12. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    Thanks for tracking that down. It looks like this has been corrected, and our QA team is working to confirm the fix now.
     
  13. slimshader

    slimshader

    Joined:
    Jun 11, 2013
    Posts:
    187
    I is not just "using static" it also happens when get-only properties are defined in custom component implementation file.
     
  14. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    Can you submit a bug report with the specific code that causes a problem? Then we can confirm that our change corrects the case you see as well.
     
  15. huyakinemota_unity

    huyakinemota_unity

    Joined:
    Feb 4, 2018
    Posts:
    3
    same problem here in Unity 2018.1. "using static" directive makes Unity no longer see MonoBehaviour script; Project uses .NET 4.*
     
    pragmascript likes this.
  16. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    Please submit a bug report, and we will investigate it. Thanks!
     
  17. SugoiDev

    SugoiDev

    Joined:
    Mar 27, 2013
    Posts:
    395
    Hey @JoshPeterson

    I confirm the issue on Unity 2018.2.3f1 with (or without) the latest Incremental Compiler.
    The problem seems to be with using static imports in types that are in a namespace.
    Types without namespace mostly work.

    I say mostly because you can still break the parser, but I found no consistent way of doing that.

    Bug report # is 1069915
     
    pragmascript and JoshPeterson like this.
  18. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    945
    This happens randomly with regular scripts for me aswell (no static using). But i also cannot reproduce it consistently (but when it happens, the 'Namespace' field in the debug-view inspector is empty).
     
  19. SugoiDev

    SugoiDev

    Joined:
    Mar 27, 2013
    Posts:
    395
    Yeah, I saw more people reporting the same issue with 2018.2.3 here on the forums.
    One guy reported that adding a space before the colon when inheriting from MonoBehaviour-derived classes fixed the issue for him (!)

    Like, SomeBehaviour : MonoBehaviour works, but SomeBehaviour: MonoBehaviour breaks the parser.
    I didn't test it because I'm not using .2.3 anymore.
     
  20. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    Thanks for the bug report, we will investigate it.
     
    pragmascript likes this.
  21. stepan-stulov

    stepan-stulov

    Joined:
    Nov 26, 2013
    Posts:
    28
    I also have a similar problem, but mine is about incorrect evaluation of #if preprocessor directives and Unity not seeing my MonoBehaviour script in a namespace behind that #if:

    With BUILD_MY_APP being a custom define symbol / preprocessor directive only under standalone build (and not WSA build) and with standalone build being active/selected...

    This doesn't work (script could not be loaded):

    Code (CSharp):
    1. #if BUILD_MY_APP && (UNITY_WSA || UNITY_STANDALONE)
    2. namespace MyCompany.MyProduct
    3. {
    4.    public class MyClass : MonoBehaviour
    5.    {
    6.       ...
    7.    }
    8. }
    9. #endif
    However this does:

    Code (CSharp):
    1. #if BUILD_MY_APP && (UNITY_STANDALONE || UNITY_WSA)
    2. namespace MyCompany.MyProduct
    3. {
    4.    public class MyClass : MonoBehaviour
    5.    {
    6.       ...
    7.    }
    8. }
    9. #endif
    Notice how the only thing changes is the order of the Unity's preprocessor directives / define symbols, which is nonsense because these two expressions should clearly produce the same result.

    I think Unity under the hood parses the preprocessor directives in a way that simply doesn't cover all edge cases and makes certain assumptions about the built-in (UNITY_) directives. This is also a regression, as all our scripts loaded correctly in an older version of Unity (I can't remember which).

    Finally, this code works (which is our current workoaround for our multi-app and multi-platform project setup):

    Code (CSharp):
    1. #if BUILD_MY_APP
    2. #if UNITY_STANDALONE || UNITY_WSA
    3. namespace MyCompany.MyProduct
    4. {
    5.    public class MyClass : MonoBehaviour
    6.    {
    7.       ...
    8.    }
    9. }
    10. #endif
    11. #endif
    I submitted a bug but I can't really find it. By the way how can I see the bugs I've submitted?

    Update: bug report 1080696

    Cheers
     
    Last edited: Sep 19, 2018
  22. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    When you submit a bug, you'll get an email notifying you about the bug #, and a link to a page containing that bug report and all other bug reports you have submitted.

    The first email usually shows up after 1-2 minutes, and is an automated email telling you that it's been received. Then, when it's handled, you'll get an email - usually just saying "we confirmed the bug and sent it to our developers for resolution", or a follow-up email asking for more info. That can take everything from hours to weeks.
     
    stepan-stulov likes this.
  23. tswierkot

    tswierkot

    Joined:
    Feb 15, 2017
    Posts:
    25
    I see this bug was fixed in 2018.2.2f1. Can we get this fix in 2017.4?
     
  24. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    We're not planning to back port this fix to 2017.4, since support for C# 6 and the new scripting runtime was experimental in 2017.4.
     
  25. ammirea

    ammirea

    Joined:
    Oct 22, 2018
    Posts:
    8
    Any word on fixing the thing? Using Unity Pro 2018.2.6f1 and still getting this bug. Pretty please, we need namespaces and long full qualifiers are not a viable option here.
     
  26. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    It should have been corrected, but maybe something else went wrong. Can you send us a new bug report for this issue you see?
     
  27. ammirea

    ammirea

    Joined:
    Oct 22, 2018
    Posts:
    8
    Thank you for replying so quickly.
    Bug report sent a few days ago. Today I've sent a basic demo project.
    I'm putting the link in here as well:
    https://we.tl/t-Im9xWvGYVg
     
    JoshPeterson likes this.
  28. ammirea

    ammirea

    Joined:
    Oct 22, 2018
    Posts:
    8
    With the assistance of Unity people at bug reporting - thank both you and them - I can confirm that the bug is solved and not present in Unity 2018.3.
     
    JoshPeterson likes this.