How to call a custom object from a C# dll


(function, file, and class names changed to protect the innocent)

So I have a C# dll named CSharpDLL

so I do this in my unity C# script.

using CSharpDLL;

// later on

private CSharpDLL.CustomObjectName foo;  // note that this works just find and doesn't cause any errors

// later on

(I press foo. and mono recognizes the object and shows me the names of all of its member functions, sweet this must be working)

foo.voidMethod();  // this causes the error

foo = new CSharpDLL.CustomObjectName();  // this causes the error and yes there is a default constructor in the dll class

Both of these create the following error in Unity :

Internal compiler error. See the console log for more information. output was: Unhandled Exception: System.TypeLoadException: Could not load type 'CSharpDLL.CustomObjectName' from assembly 'CSharpDLL, Version=, Culture=neutral, PublicKeyToken=null'.

Note that my CSharpDLL is in the plugin folder.

Question : How do I create an object that is defined in a C# dll and use it?


Don't put managed C# dlls into the plugin folder. The plugin folder is for native code dlls. If you compiled a "normal" C# dll, unity will import it like any other asset. Unity will strip away the namespace and import the classes into the global namespace (that happends at least with classes that are derived from MonoBehaviour). I'm not sure if all classes are handled that way. I've done only some tests with dlls and unity.


I've just created a test dll that looks like this:

using System;
using System.Collections.Generic;
using UnityEngine;

namespace ClassLibrary1
    public class DLLTest
        public DLLTest()
            Debug.Log("Hello World");


And in Unity when i execute this line i get the "Hello World" printed.

ClassLibrary1.DLLTest tmpObj = new ClassLibrary1.DLLTest();

As i said, just place the dll somewhere in your assets. If it contains classes that are derived from MonoBehaviour it will show up in the project view as sub-assets of your dll, so you can drag-drop them onto GameObjects