Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Scripting' started by Zephni, Apr 23, 2015.

  1. Zephni

    Zephni

    Joined:
    Apr 23, 2015
    Posts:
    100
    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 :)
     
  2. christinanorwood

    christinanorwood

    Joined:
    Aug 9, 2013
    Posts:
    402
  3. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    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 ^^
     
  4. Zephni

    Zephni

    Joined:
    Apr 23, 2015
    Posts:
    100
    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!
     
  5. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    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
     
  6. Zephni

    Zephni

    Joined:
    Apr 23, 2015
    Posts:
    100
    Oooops! Sorry Christina!! :p I'll check your website out outward, thanks :)
     
  7. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    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.