Datatyping exposed variables

I have created a class called ‘Controller’. From another class, I then declare:

public Controller myController;

But Unity tells me that the ‘type or namespace Controller could not be found’. Is there a special place on the filepath that classes need to go in order to be recognised by Unity?

If you’ve actually created a class (and you’re using js), you should be able to do this:

var myController = Controller();

Sorry, I should have said I’m using C#

the only requirement is that controller was either compiled in a previous step or the same. Check out the compile order documentation thats part of the advanced ref

I’ve checked out those docs and tried every combination I can think of. Unity either says it can’t find the type, or it gives this warning:

You are trying to create a MonoBehaviour using the ‘new’ keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all

My controller class doesn’t inherit from anything at the moment. If I make it inherit from ScriptableObject it tells me it can’t find the type ‘ScriptableObject’.

I’m starting to think there’s something wrong with my copy of Unity. All I’m trying to do is create a class and instantiate it.

I’m FAR from an expert in C#, but I think the script where you define a class has to have the same name as the class itself.

To instantiate an object from this class in another script, simple do:

Controller myController = Controller();

I’m finding that Unity is not keeping up with edits that I make in Unitron. Errors will persist unitl I quit them both and reload.

You can set the console to clear errors on play (or manually clear them).

And if you’re using an older Mac (one without an intel processor), it can take FOREVER for scripts to reload after saving them.

If your class inherits from MonoBehaviour (by default, they always do) then you can’t create an instance using the new operator. Just to be sure, click on the error bar at the bottom of the window to open the console. Check that you aren’t getting any compile errors with the Controller file that are being “hidden” by the other error.