Make an instance of a class that does not extend MonoBehaviour and not a component

Hi guys,

Still pretty new to Unity, I created a few classes purely for data structure and the data itself, but soon came across some problems when trying to instantiate my classes as there are issues when doing “new MyClass()” if it extends MonoBehaviour or is a component of a Unity object.

I hope your still with me…

So I created the classes as standard files in the assets folder that DON’T extend MonoBehaviour. But what seems strange is there is no namespace associated with my game, so I’m not sure how to make the classes accessible from my GameObject component classes. Any help here would be hot coz’ I’m clearly a little lost…

Thanks in advance for any answers :slight_smile:

Use Assets as the namespace, and additionally any subdirectory of Assets that your code is in. Terry Norton does this a lot in his book http://www.amazon.com/Learning-Developing-Games-Unity-Beginners/dp/1849696586

Can you post one of these classes here so I can help you out a little better. I’ll need more information for your specific issue. What she said might work ^^

Thanks Christian, and outwarddesign for your replies! I did what Christian said and put the classes in their own namespace and then just referenced the namespace in the component scripts that needed them.

So wrapped the custom classes in:
namespace Assets {
//Classes etc.
}

And then put this at the top of my MonoBehaviour (or component classes)
using Assets;

Thanks again guys for your help!

No worries. If you need any help in the future feel free to get at me via my website or Twitter account. Links in my signature. Also, I think Christian is a Christina :-p

Oooops! Sorry Christina!! :stuck_out_tongue: I’ll check your website out outward, thanks :slight_smile:

You could have also just taken off the monobehavior in the class, hehe. Then if you have Class somevar = new Class(); you wouldn’t get that error. If you want a gameobject to be able to access the structure, instantiate that structure in a monobehavior that’s on the gameobject. If you have some utility functions you’d like your gameobjects to have access to, make them static. A less desirable option for the last bit is to use a singleton.