Component.active is obsolete but no enabled property

Hi,

I’m getting this warning:

“Component.active is obsolete”

However there’s no corresponding enabled property for Components like there is for GameObjects. What am I missing?

Component.enabled

Components must be enabled whereas objects have to be activated.

Erm, that’s exactly what I was looking for but:

'UnityEngine.Component' does not contain a definition for 'enabled'

That’s what’s so confusing.

“Component” is merely the class name for a component used by a game object. When I wrote “component” I meant to use whatever component your script is trying to access.

For example, if your script is trying to access a light component in a game object, you write “light.enabled” since light is the component. Accessing a component this way is the same for every other component as well (except scripts).

Sorry about the confusion.

I understood you the first time, I’m familiar with the distinction between classes and their instances :wink:.

The Light class only has an ‘enabled’ property because it derives from Behaviour (which subclasses Component).

The base Component class does not have an enabled property, only the subclass Behaviour does.

To make it clearer, the following complains about an obsolete “active” property:

        Component c;
            <snip>
        c.active = false;

The following however, will not compile:

        Component c;
            <snip>
        c.enabled = false;

What’s confusing is Component complaining about ‘active’ being obsolete while it clearly does not have an enabled property superceding it.

Your own classes can’t extend component, you extend MonoBehaviour.

For all the unity integrated components, enabled exists, they are not straight Component descendants too, but Behaviour extends which adds enabled

Hi dreamora, I’m not trying to extend Component;

what I have/had is code similar to the following:

Component[] components;
components = GetComponentsInChildren(typeof(<someclass>));

foreach(Component target in components)
{
    <inane code goes here>
    target.active = false;
}

…that give unnecessary warnings about the obsoleted active property.

I just found it curious; I can ignore it.

However if ‘active’ on Component was deprecated without having a corresponding ‘enable’ property available for a reason, I’d of course like to know early so I can refactor the above code to avoid it. Right now the warning just looks like an oversight/minor bug, hence why this is in Support as opposed to Scripting.

As mentioned it is present, but you use the wrong class.
Behaviour is the right one, not components.

By downcasting to components you lose the enabled field.

All samples don’t have that problem because JS will not use components, it will automatically use Behaviours, its the C# devs that need to keep the correct class in mind if they don’t want to get struck by the inexistant field.

Component is not used for any internal component that you could disable. Those who directly extend from component can be removed or are always active.

Ah, I get what you’re saying.

So Components (aside from Behaviours) are not meant to be enabled/disabled and are supposed to be always active; that clears up the design for me. That also explains why some components have the enabled checkbox and some don’t.

Thanks dreamora!

Ah… good. Finally found solution to my enabled problem. I also think that the compiler error is misleading.

I tried this first:

_Camera.GetComponent("SmoothFollow").active = true;

…and then you get:

‘UnityEngine.Component.active’ is obsolete: ‘“the active property is depricated on components. Please use GameObject.active instead. If you meant to enable/disable a single component use enabled instead.”’

So, I tried this:

_Camera.GetComponent("SmoothFollow").enabled = true;

…which leads to:

‘UnityEngine.Component’ does not contain a definition for ‘enabled’…

Not until I found this thread, I was able to do the thing correctly:

((Behaviour)_Camera.GetComponent("SmoothFollow")).enabled = true;

The compiler warning on depricated active should be clarified to point to ‘Behaviour.enabled’.

/Ricky