Hefaistus
Hefaistus

Reputation: 41

.NET Standard 2.0 reference not recognised in Unity

I am currently experimenting with the Hololens in combination with Unity. Part of the experiment is exchanging data with an existing .NET Framework application. This means that I have a number of libraries that are shared by both applications, which I have rewritten into .NET Standard 2.0 libraries so that they are compatible with both applications.

However, when I start referencing the .NET Standard 2.0 library in a Unity script, Unity gives the following error:

error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral'.

I am having a lot of trouble figuring out what is causing this. When I check Edit -> Project Settings -> Player, I have Scripting Runtime Version set to .NET 4.x Equivalent; Scripting Backend to .NET; and Api Compatibility Level to .NET Standard 2.0. When I double click the error, it opens the Assembly-CSharp assembly in Visual Studio, where it shows v2.0 of netstandard among its references. When I lower the target of the libraries to .NET Standard 1.4, I no longer have the error, but unfortunately I have to use 2.0.

What do I need to set in order for Unity to recognise that v2.0 of netstandard is referenced?

I use Unity 2018.1.6f. I have previously asked my question on Unity Answers, but it saw no responses there.

Thanks for reading.

Edit: Per request, here is the code in the Unity script:

using AugmentedReality.Device;

public class ServiceProvider
{
   #region Singleton
   private static ServiceProvider _instance;
   public static ServiceProvider Instance
   {
      get
      {
         if(_instance == null)
            _instance = new ServiceProvider();
         return _instance;
      }
   }
   #endregion

   public IOwnShipService OwnShipService;

   private ServiceProvider()
   {
      OwnShipService = new OwnShipService();
   }
}

And the OwnShipService in the .NET Standard 2.0 library:

namespace AugmentedReality.Device
{
   public interface IOwnShipService
   { }

   public class OwnShipService : IOwnShipService
   {
      public OwnShipService()
      { }
   }
}

This is enough to cause the error. I kept reducing the amount of code called to see what was causing it. Simply instantiating an empty class from the library is enough to trigger the error in Unity. I have included a screenshot of the error as well.

Upvotes: 4

Views: 3057

Answers (1)

slaphshot33324
slaphshot33324

Reputation: 658

Try using the IL2CPP scripting backend. This has a much larger .NET surface then the .NET scripting backend and the .NET scripting backend is being deprecated anyways.

Upvotes: 1

Related Questions