Search Unity

Unity complains about missing assembly reference when using external DLL

Discussion in 'Scripting' started by Julien_, Mar 18, 2015.

  1. Julien_

    Julien_

    Joined:
    Nov 12, 2013
    Posts:
    43
    Hi guys,

    I'm trying to use an external dll with my Unity project. It is a native .net 2.0 library. I'm using MonoDevelop and set up the reference to my dll through 'Project->Edit References' and add the .net assembly there. Monodevelop builds fine, but Unity complains about a missing assembly reference.

    I read, that other people had this problem and copied the dll into their Assets folder, so I did that. It didn't help, the error message is still there and Unity just won't compile my script.

    Additionally each time I close and reopen MonoDevelop the reference to the library is gone and I need to add it again. But I shouldn't even need to add it as a reference if I have the dll in my Assets folder, right?

    I'm out of ideas and I appreciate any help.

    Cheers
    Julien
     
  2. AngryAnt

    AngryAnt

    Keyboard Operator Moderator

    Joined:
    Oct 25, 2005
    Posts:
    3,042
    To include .net assemblies, simply drop them somewhere in your Assets folder and Unity will set up the MonoDevelop assembly reference for you.

    Generally, Unity only knows about things in the Assets folder and things included in Unity.
     
  3. Julien_

    Julien_

    Joined:
    Nov 12, 2013
    Posts:
    43
    Hey, thanks for your reply.

    However as I wrote in my initial post, just dropping it into the Assets folder isn't working for me. Unity doesn't set up the reference. So I get the typical "error CS0246: The type or namespace name `Ixxat' could not be found. Are you missing a using directive or an assembly reference?"
    But the assembly is clearly there in the Assets folder. Am I missing something?
     
    Last edited: Mar 18, 2015
  4. AngryAnt

    AngryAnt

    Keyboard Operator Moderator

    Joined:
    Oct 25, 2005
    Posts:
    3,042
    This error message is generated by Unity in the console? Where are you putting the assembly and where is the script trying to use the assembly located?
     
  5. Julien_

    Julien_

    Joined:
    Nov 12, 2013
    Posts:
    43
    Yes, it's a Unity error message. I tried both Assets and Assets/Plugins, none worked. My script is located in Assets/Scripts
     
  6. AngryAnt

    AngryAnt

    Keyboard Operator Moderator

    Joined:
    Oct 25, 2005
    Posts:
    3,042
    If you ask Unity to regenerate the MonoDevelop solution (via the assets menu), does the assembly correctly show up as a referenced external assembly in the solution?

    Should that indeed be the case, we're running out of runway here. Remaining guesses without more data are basically that either you're trying to access the wrong namespace - one which is in fact not defined in the assembly at hand - or the assembly is of an incompatible IL version.
     
  7. Julien_

    Julien_

    Joined:
    Nov 12, 2013
    Posts:
    43
    Sync MonoDevelop Project also doesn't make the assembly show up in the solution. If I manually add it as reference beforehand it even makes it disappear again.

    What I'm trying to do specifically: I'm trying to communicate with a device over a CAN-to-USB interface:
    http://www.ixxat.com/download_vci_v3_en,20983,5873.html

    A .Net API is provided and I'm able to compile sample code in Visual Studio to communicate with the device. But I want to have the data in Unity and since it's a .Net 2.0 assembly I thought, it should be possible to access it directly from a script in Unity.

    Since that didn't work I wrote a simple C# Wrapper in Visual Studio compiled it to a dll (to get around the namespacing issue). When I tried using that dll in Unity instead, I got an "Unhandled Exception: System.TypeLoadException" error message.
    I don't know if the two problems are related.

    Again, I'm glad you're taking time to look into this, thanks a lot.
     
  8. AngryAnt

    AngryAnt

    Keyboard Operator Moderator

    Joined:
    Oct 25, 2005
    Posts:
    3,042
    Could you check the Unity log file for any further information on that error? http://docs.unity3d.com/Manual/LogFiles.html

    Additionally, can you verify the .net assembly versions of all the non-built-in assemblies referenced by the wrapper assembly?

    And finally, which built-in assemblies are references? Perhaps there's a dependency not included in the mono profile employed by Unity?
     
  9. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,562
    Sounds to me like that API DLL is a com wrapper used to access an unmanaged API. It may be trying to load other types via com or reflection that it is unable to load, such as native or .net classes that are not part of unities mono distribution or possibly winapi stuff. My guess though is that it contains or accesses unmanaged code and for some reason can't be loaded by unity.
     
  10. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,562
    The off manual says "The .NET 2.0 API is only used for adaptation to the COM-based programming interface". It would be a bit of work but you could write a windows service to install alongside your unity app and use that service as a proxy to interface with the driver. Your wrapper would then communicate with the service. Your service would use the API DLL and communicate with the CAN driver.
     
  11. Julien_

    Julien_

    Joined:
    Nov 12, 2013
    Posts:
    43
    Ah I see, I should've read the manual more carefully.

    I feared that this would be the way I need to go to solve this. Thanks for clarifying.

    For now I'm using a different approach however. The device doesn't have to use the CAN communication protocol. I'm now using a serial COM port instead. This takes a bit of redoing work on the hardware side of things but it's easier to implement in Unity, with .NET SerialPort.

    Anyway, thanks a lot for your help, I really appreciate it.
     
  12. RickBeniers

    RickBeniers

    Joined:
    Sep 25, 2018
    Posts:
    4
    hello guys

    I have the same issue when trying to use LitJson to create an inventory system for my 3D RPG game, I already added the LitJson.dll file into the Assets folder and added LitJson.dll to my visual studio script through references. unity won't remove 2 errors in the console and because of that I can't run my scene or create new scripts. Do I also need to use a Com port? and if so, how? are there any other solutions? @AngryAnt @Julien_ @Dustin-Horne

    PS: I found this solution/answer on a different forum but it doesn't help much.
    http://answers.unity.com/comments/1185483/view.html

    LitJson-error02.png LitJson-error.png
     

    Attached Files:

    Last edited: Jan 25, 2019
  13. tgrotte

    tgrotte

    Joined:
    Apr 17, 2019
    Posts:
    1
    I was getting the same error because I had differing target frameworks between my dll and those specified in the Player settings. Once I matched those up the problem went away.