C#6 using static problem

Hi.
I found a editor bug that I could not add component when using using static with a namespace definition.

using static System.Math;

namespace Test
{
    public class A : MonoBehaviour {

        :
    }
}

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.

1 Like

Which version of Unity are you using in this case?

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.

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

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.

Same here,
2017.3.1p3

Can you submit a bug report for this issue? We would like to correct it.

@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:

using static System.Math;
namespace Test
{
    public class A : MonoBehaviour {
        :
    }
}

I’ve just tried this in 2018.1, and it looks to be working correctly. Can you try it in that version?

I’m having this problem in 2018.1 beta 13.

Something like the below will work.

using UnityEngine;
using static UnityEngine.Mathf;

public class Tester : MonoBehaviour {
    
}

However the below does not.
Basically namespaced MonoBehaviours are broken

using UnityEngine;
using static UnityEngine.Mathf;

namespace DeathByNamespace
{
    public class Tester : MonoBehaviour
    {
       
    }
}
1 Like

@JoshPeterson I found this besides @MoncGreen providing a validated test case:

I guess it’s still in the process of being implemented into a future release.

Thanks for tracking that down. It looks like this has been corrected, and our QA team is working to confirm the fix now.

I is not just ā€œusing staticā€ it also happens when get-only properties are defined in custom component implementation file.

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.

same problem here in Unity 2018.1. ā€œusing staticā€ directive makes Unity no longer see MonoBehaviour script; Project uses .NET 4.*

1 Like

Please submit a bug report, and we will investigate it. Thanks!

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

2 Likes

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).

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.

Thanks for the bug report, we will investigate it.

1 Like