[public property of custom class] is not a member of "Object"

Hi,

I have a generic variable defined as:

var obj : Object;

Previously, I would assign various objects of different types to that variable and access their common properties.

For example, I have objects of class ballObj and objects of class cubeObj. Both of those classes contain a public “isEnabled” property.

When #pragma strict is NOT turned on, attempting to access obj.isEnabled (after an object of either of the above classes has been assigned to it), there is no error.

However, when #pragma strict IS turned on, trying to access .isEnabled returns the following error:

“isEnabled” is not a member of “Object”.

How can I get around this, without strongly typing the variable “obj” to either the ballObj or cubeObj type?

Thanks!

You need a strong type base. You can either make a base-class that actually is derived from MonoBehaviour and derive your other classes from this one. That way you can use you base class as type instead of “Object”. The other way would be to use an interface, but i’m not sure how to implement those in UnityScript. I’m not sure if it’s even possible in US but it would be my prefered method :wink:

Just had a quick search and found this one. So it is of course possible to derive your class from an interface but it seems you can’t declare them in UnityScript.

In your case i would use a common base class:

class MyBaseClass extends MonoBehaviour
{
    var isEnabled : boolean;
}

Declare your variable like this:

var Obj : MyBaseClass;

And your other classes that should be able to be assigned to that variable have to inherit from your base class:

class BallObj extends MyBaseClass
{
    
}

Using your Obj variable you can of course only access the base class members / functions that have been inherited.

Just a little hint, try to avoid using the word property for member variables. Properties in .NET / Mono and even a lot other languages are something totally different.