To enable or make active? I am at a loss here!

So I have various behaviors tucked into their own little script each and depending on the situation I want to enable certain components while disabling others. I.e. make my guard go on patrol or make my patrolling guard go into hunter mode after spotting the enemy.

So I do this:

function UpdateSteering()
{
	switch (SearchMode)
	{
		case eSearchMode.None:
			TargetCode.active = false;
			WanderCode.active = true;
			TetherCode.active = true;
			break;
			
		case eSearchMode.Junk:
		case eSearchMode.Player:
			WanderCode.active = false;
			TetherCode.active = false;
			TargetCode.active = true;
			break;
			
		case eSearchMode.Success:
		case eSearchMode.Inspecting:
			TargetCode.active = false;
			WanderCode.active = false;
			TetherCode.active = false;
			break;
	}

Now Unity gives me a warning that

So I change it all…

function UpdateSteering()
{
	switch (SearchMode)
	{
		case eSearchMode.None:
			TargetCode.enabled = false;
			WanderCode.enabled = true;
			TetherCode.enabled = true;
			break;
			
		case eSearchMode.Junk:
		case eSearchMode.Player:
			WanderCode.enabled = false;
			TetherCode.enabled = false;
			TargetCode.enabled = true;
			break;
			
		case eSearchMode.Success:
		case eSearchMode.Inspecting:
			TargetCode.enabled = false;
			WanderCode.enabled = false;
			TetherCode.enabled = false;
			break;
	}
}

Lordy lordy lord… guess what? I have an entire project with 0 errors AND 0 warnings!!! It’s amazing! 6 months on a project with a massive amount of scripts and not even as much as a single warning in my entire project! That’s how good I am… I rock! Yeah baby…!

But lark! Behold! Lo what is thus?

Then I notice “Oh, my word. I don’t even have a pragma strict on this script” so I add it and what do you know…?

Now I have 9 errors in my script saying that

So WTF? Unity gives me a warning saying I should do something that results in an error instead… For months now I have been biting my thumb at Unity each time it says “This is depreciated” because it still works and their way doesn’t… all the time fearing that the next release is the one where they say “We’ve given you a year and half to fix up your project, now those depreciated actions will no longer be supported”.

So now I ask you with tears in my eyes, can’t someone please explain to me the simple rule of when to enable and when to activate a gameobject/ script/ component as I hate getting warnings but it seems using either enabled or active will results in either warnings or errors and there is just no way around it… and it’s driving me insane trying to find ways to get around it… adding extra lines of code to type cast stuff and adding variables to simply return from the Update functions instead of completely disabling the component…It really is getting to be real frustrating!

Please help

Thanks

Easy rule is that you use 'active 'on GameObjects and use ‘enabled’ on Behaviours. The errors you are getting are because you are using the result of GetComponent which returns a Component. Component does not have a variable called enabled. This is because GetComponent can also return other components which are not behaviours like transforms, colliders, etc. To solve your problem, you need to cast the result of GetComponent to at least Behaviour, or more properly MonoBehaviour for your code to work.

In this case, I would agree that simply changing from Component to something else could work, but as it is, I didn’t use the result of GetComponent and got a type I didn’t want…I actually manually specified Component and dragged the components onto the fields BECAUSE I wanted the Component type.

For what I wanted to do this would have been ideal… The problem (in this case) is that:

Unity warns me not to use active, but to use enabled instead, saying that active is depreciated on Components. Then when I use enabled on a component it tells me that enabled is not a member of Component. So I need to go back to active where Unity once again warns me that active is depreciated for Components and I should use enabled.

Why? Is this a bug or something or are we not actually intended to ever use the Component class in our scripts?

Like I said, I have in past gotten around these issues as there are multiple ways to solve any issue. I am not saying this issue is insurmountable. I am just asking why do I get a warning that tells me to cause an error? Is it me or is it them? Am I always to typecast a component just to turn it off? Or should that warning just be made to not appear in that instance?

It frustrates me to not be able to get a clean log unless I modify working code to continue working but only after 2 or three extra lines of code or doing something equally mundane just to take working code and making it function exactly the same was as it was working beforeI made the mundane change to the code that was working in the first place. Apart from my frustration at this, this is not a serious, end of the world issue, I am just wondering…

I want to use a Component class but the enable/active rule doesn’t allow me to without getting either a warning or an error. Am I not supposed to ever use Component / am i doing something else wrong or is this a minor bug?

No, it’s just that by enabling Pragma Strict, you disable dynamic casting/typing, which means that you must specify exactly what you are modifying by object type.

Admittedly, the Unity error messages aren’t that helpful sometimes, but this isn’t a bug. Components do not have the enabled property. Look at the documentation - it dosen’t exist. It does exists on Behaviors, which are what your scripts inherit from, and therefore what you are actually trying to do. Cast all of the calls to enabled as Behaviors instead of Components and it will work.

Unity is not wrong, you simply need to be more specific in your casts - that’s what Pragma Strict does. It may seem very annoying, but it is best practice. Specifying the object as a Behavior rather than a Component ensures you can never make the call to enabled when that property does not exist.

(I haven’t explained this very well, hopefully someone else can…)

Why do you need to use the Component class specifically? And not Behaviour or MonoBehaviour which inherit from Component? From what I am seeing in the code you posted, you are most likely only using MonoBehaviours there. You can still drag components in unity onto those fields.

No one is stopping you from using the Component class, but in the past Unity decided that the active field shouldn’t be in the Component class but should be the enabled field in one of it’s children. The reason the deprecated warning suggests using enabled is because most people encountering that warning are using Behaviours anyway and thus are able to simple use enabled. You are one of the few exceptions that want to use the Component class (for which I can’t really think of a good reason to use unless you also have colliders in the same list).

Nah, I guess the problem was on my side this time as my stubborn desire to WANT to use Component. Like I said, I just want to turn the class on or off. That is entirely it. I didn’t see the need to go anything above Component but since the classes I want to enable/disable all derive from MonoBehavior anyway I guess there is no reason to NOT use that instead of Component. I just find it weird that there is no legal way to disable a component except through the derived classes.

Like Jevans said, the is no enabled property, I get that… but no legal use of active either? Well, I guess I can buy your explanation. Unity decided that almost nobody would ever want to use Component directly so decided everyone won’t be allowed to and with me always doing things my own way I am now trying to do something I am not allowed to. I can buy that…

As to your question for why, it is because those components are all in c# and my script is in JavaScript. In c# you can call GetComponent and it will return the components wether they are enabled or not, but in JS GetComponent only returns active components. As such, when I disable a script there is no way for me to turn it back on again without a direct reference. Since JS classes don’t see c# classes unless you place them in assets/plugins and I don’t want to mess up my folder layouts just for 4 scripts that get special treatment, I decided to just use component just to get a reference. Again, I can’t use the ACTUAL type and I just want to turn the darn component back on again, nothing fancy at all… So I randomly picked a class I knew the components would all have, Component. :stuck_out_tongue:

I ran the project and it worked… Then I enabled the pragma and Unity starts bitching about my code… The way I look at it, Component is still fine to use since the active does do what it is supposed to. Just the error message shouldn’t display… If it wants to you use enabled on a subclass of Component then the message should read “active is depreciated on Component. If you want to turn off an individual component, use enabled ON A CLASS DERIVED FROM Component”.

Okay, I can’t recall any of the other cases right now but at least this one I can admit the mistake is partly mine. At least by changing my Components to MonoBehaviors is an elegant enough fix for this little issue :slight_smile: Thanks for the hints