Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Miivers · Nov 01, 2011 at 09:47 PM · c#dllmanaged

Using dll's from C#?

Hello

I am trying to use a dll I made in C# .net 2.0 without any luck. From what I have read, the dll should be placed in the asset folder and can then be used as any other scripts.

My Dll:

 using UnityEngine;
 using System;
 using System.Collections.Generic;
 using System.Text;
 
 public class SerialGPS_DLL2 : MonoBehaviour
 {
     public void SerialGPSInit(string comPort, int baudRate)
     {
         //Some code
     }
 
     public void SerialGPSClose()
     {
         //Some code
     }
 
     public void SerialGPSUpdate(ref double latitude, ref double longitude, ref string status)
     {
         //Some Code
     }
 }


How I use it: Adding dll as referance in Visual Studio(just to get intellisense).

 using UnityEngine;
 using System.Collections;
 using System.Runtime.InteropServices;

 public class SerialGPS : MonoBehaviour 
 {
     private SerialGPS_DLL2 m_cSerialGPS_DLL;
     
     void Start () 
     {
         m_cSerialGPS_DLL.SerialGPSInit("COM3", 4800);
     }
 
     void Update()
     {
 
     }
 }


The error i get:

 Internal compiler error. See the console log for more information. output was:
 Unhandled Exception: Mono.CSharp.InternalErrorException: Assets/SerialGPS.cs(5,14): SerialGPS ---> Mono.CSharp.InternalErrorException: Assets/SerialGPS.cs(12,28): SerialGPS.m_cSerialGPS_DLL ---> System.TypeLoadException: Could not load type 'SerialGPS_DLL2' from assembly 'SerialGPS_DLL2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
   at (wrapper managed-to-native) System.Reflection.Assembly:InternalGetType (System.Reflection.Module,string,bool,bool)
   at System.Reflection.Assembly.GetType (System.String name, Boolean throwOnError, Boolean ignoreCase) [0x00000] in <filename unknown>:0 
   at System.Reflection.Assembly.GetType (System.String name) [0x00000] in <filename unknown>:0 
   at Mono.CSharp.SimpleName.Error_TypeOrNamespaceNotFound (IMemberContext ec) [0x00000] in <filename unknown>:0 
   at Mono.CSharp.SimpleName.ResolveAsTypeStep (IMemberContext ec, Boolean silent) [0x00000] in <filename unknown>:0 
   at Mono.CSharp.Expression.ResolveAsBaseTerminal (IMemberContext ec, Boolean silent) [0x00000] in <filename unknown>:0 
   at Mono.CSharp.Expression.ResolveAsTypeTerminal (IMemberContext ec, Boolean silent) [0x00000] in <filename unknown>:0 
   at Mono.CSharp.MemberBase.ResolveMemberType () [0x00000] in <filename unknown>:0 
   at Mono.CSharp.MemberBase.Define () [0x00000] in <filename unknown>:0 
   at Mono.CSharp.Field.Define () [0x00000] in <filename unknown>:0 
   at Mono.CSharp.TypeContainer+MemberCoreArrayList.DefineContainerMembers () [0x00000] in <filename unknown>:0 
   --- End of inner exception stack trace ---
   at Mono.CSharp.TypeContainer+MemberCoreArrayList.DefineContainerMembers () [0x00000] in <filename unknown>:0 
   at Mono.CSharp.TypeContainer.DefineContainerMembers (Mono.CSharp.MemberCoreArrayList mcal) [0x00000] in <filename unknown>:0 
   at Mono.CSharp.Class.DefineContainerMembers (Mono.CSharp.MemberCoreArrayList list) [0x00000] in <filename unknown>:0 
   at Mono.CSharp.TypeContainer.DoDefineMembers () [0x00000] in <filename unknown>:0 
   at Mono.CSharp.Class.DoDefineMembers () [0x00000] in <filename unknown>:0 
   at Mono.CSharp.TypeContainer.Define () [0x00000] in <filename unknown>:0 
   at Mono.CSharp.ClassOrStruct.Define () [0x00000] in <filename unknown>:0 
   at Mono.CSharp.Class.Define () [0x00000] in <filename unknown>:0 
   at Mono.CSharp.RootContext.PopulateTypes () [0x00000] in <filename unknown>:0 
   --- End of inner exception stack trace ---
   at Mono.CSharp.RootContext.PopulateTypes () [0x00000] in <filename unknown>:0 
   at Mono.CSharp.Driver.Compile () [0x00000] in <filename unknown>:0 
   at Mono.CSharp.Driver.Main (System.String[] args) [0x00000] in <filename unknown>:0 


My only theory at the moment is that I am building my dll in a way that is not supported.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Skjalg · Nov 01, 2011 at 09:47 PM 0
Share

The couple of first things I would try would be to put the DLL in the standard assets folder (as that gets compiled first), or maybe use c# 2.5 since that is the supported version. or maybe make your class public.

It should automatically be added to your solution if you've done it correctly.

avatar image Miivers · Nov 01, 2011 at 10:19 PM 0
Share

Thanks for the reply.

I noticed the missing public in front of my class just before you replayed. Sadly it did not change the error I am getting.

$$anonymous$$oving the dll to standard assets did not change anything.

Building as .net 2.5 did not help.

I can see my dll in my project tree in Unity, but the Inspector does not display anything when I select it(not sure if it is suppose to displays anything).

4 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by evilrobot · May 21, 2012 at 05:25 PM

Yeah... lame... really... really... LAME error message. Just cost me several hours while I'm on a deadline. My .Net common libraries reference SYstem.IO.Ports... I need them to debug a serial peripheral device.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by jeremyace · Nov 01, 2011 at 10:48 PM

You can drop .Net DLLs in your project, compiling order has no effect here as all Unity scripts are also compiled into .Net DLLs before the scene is run.

However, I think your issue comes from creating an instance of MonoBehaviour that way. Remove the MonoBehaviour subclass part from the SerialGPS_DLL2 class definition and try it.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Miivers · Nov 02, 2011 at 01:33 AM

I striped the dll for all functionality and found out it didn't like the System.IO.Ports namespace I was using.

After testing System.IO.Ports in a new class in Unity i got the error: "the type or namespace 'port' does not exist in namespace 'System.IO'".

So my problem does not seem to be dll related after all. Found a post about System.IO.Ports here.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by shrek · Nov 02, 2011 at 05:23 AM

in the player settings change the .NET 2.0 subset to .NET2.0 it will work..

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using Native C++ dll in Unity 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

c# dll works on desktop but not on android 1 Answer

Managed Plugin Can't Reference Unity Plugins 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges