x


AddComponent for MonoBehaviour in DLL plugin does not work..

So I have a DLL with a class ResourceManager implemented as a singleton. The GetInstance() method for the class has the following code..

public static ResourceManager GetInstance()
{
    Debug.Log("ResourceManager Inst");
    if (cInstance == null) {
        GameObject container = new GameObject("ResourceManager");
        cInstance = container.AddComponent<ResourceManager>();
    }
    return cInstance;
}

where cInstance is defined as..

static ResourceManager cInstance;

This is all happening inside a DLL which is placed in the Plugins folder of my Unity project. So when I make the following inside a script in my Unity scene,

ResourceManager.GetInstance().DoSomething();

I get the following error..

"Can't add component because 'ResourceManager' is not derived from Component."

So, is it the case that, this kind of code just will not work because it is in a dll or is there something wrong in the way I have done things here?

NOTE: as of Unity 3.x this is now possible.

more ▼

asked Jan 13 '10 at 05:46 PM

lokstok gravatar image

lokstok
145 4 7 14

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

unity does not support components in external assemblies. a class library (DLL) is an external assembly and you can not have a component in it.

more ▼

answered Jan 13 '10 at 08:46 PM

Ashkan_gc gravatar image

Ashkan_gc
9.1k 33 56 117

I am a bit confused.. I guess MonoBehaviours are components, and we are allowed to have MonoBehaviours in DLLs.. Is it just that we can't add or get components using the usual methods in these DLLs?

Jan 13 '10 at 09:38 PM lokstok

you can not add a component that is in an external assembly. see the comments of a post called "donwloading hydra"in angry ant's website http://www.angryant.com the post is not about external assemblies and components. it's about downloading code and executing it but in comments you can find what you want.

Jan 14 '10 at 02:10 PM Ashkan_gc
May 27 '11 at 07:53 AM Quazistax
(comments are locked)
10|3000 characters needed characters left

As the error says, components need to derive from the Component class. For scripts inside unity, that means you have to define your class like this:

public class MyClass : Component
{
  ...class body...
}

or, as is more commonly used (and is the default when unity creates a new c# script):

public class MyClass : MonoBehaviour
{
  ...class body...
}

This works because MonoBehaviour derives from Behaviour, which itself derives from Component.

Whether or not you can do this in your DLL, I'm not sure. I'm guessing you would have to add the UnityEngine.dll to your project, if it's not added already.

more ▼

answered Jan 13 '10 at 05:54 PM

duck gravatar image

duck ♦♦
40.9k 92 148 415

I have derived from my ResourceManager class from MonoBehaviour. The class is defined as

public class ResourceManager : MonoBehaviour

Also the dll project does include the UnityEngine.dll else it wouldn't have compiled..

Jan 13 '10 at 06:22 PM lokstok

ok - just had to check! In that case I have no idea.

Jan 13 '10 at 08:40 PM duck ♦♦
(comments are locked)
10|3000 characters needed characters left

Here people

I found a way to do that... not a clean solution but it works nice!

the laboratory blog

more ▼

answered Apr 30 '10 at 04:37 PM

Eduardo Costa gravatar image

Eduardo Costa
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x392
x230

asked: Jan 13 '10 at 05:46 PM

Seen: 2800 times

Last Updated: Nov 23 '12 at 08:55 PM