4
\$\begingroup\$

I'm using Unity 5 and want to disable a script called Unit based on whether the instance is a client or a server. I have tried using:

GetComponent("Unit").enabled = false;

However I get hit with this error:

Assets/Scripts/DisablingUnitScriptIfPlayer.cs(14,46): error CS1061: Type UnityEngine.Component' does not contain a definition for 'enabled' and no extension method 'enabled' of type 'UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)

Looking at some tutorials, this worked in previous versions, but I can't find how to make this work for Unity 5. What do I need to change?

\$\endgroup\$

3 Answers 3

7
\$\begingroup\$

Please check solution described here

For your case it would be :

(GetComponent("Unit") as MonoBehaviour).enabled = false;

Warning this will crash if the "Unit" component is not deriving from MonoBehavior (if this is a script you wrote, it should ...)

EDIT:

The solution proposed by Lohoris in comments is much cleaner and faster, and actuall the official way to do it fom the doc:

It is better to use GetComponent with a Type instead of a string for performance reasons. Sometimes you might not be able to get to the type however, for example when trying to access a C# script from Javascript. In that case you can simply access the component by name instead of type.

So the good code is :

GetComponent<Unit>().enabled = false;

Which will still of course crash if your object has no Unit component.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ This is ok but this syntax is really bad… you should be using GetComponent<Unlit>().enabled instead. Never use strings if you don't have to. \$\endgroup\$
    – o0'.
    Commented Aug 17, 2015 at 19:41
2
\$\begingroup\$

If you need a safe version of @VB_overflow's answer, you could do this:

MonoBehaviour unit = (GetComponent("Unit") as MonoBehaviour);
if (unit != null)
{
     unit.enabled = false;
}
else
{
     Debug.Log("<color=red>Error:</color> Unit behavior not found, can't disable");
}

Here's another version using strong typing based on a comment from @Lohoris, this is more recommended as it is not as fragile to changing of said class:

MonoBehaviour unit = GetComponent<UnitBehaviorClass>();
if (unit != null)
{
     unit.enabled = false;
}
else
{
     Debug.Log("<color=red>Error:</color> Unit behavior not found, can't disable");
}

That way, if it does blow up because of a code change down the road, you'll know why and where to look. Practicing safe handling of possible null references is a good practice to get in the habit of!

\$\endgroup\$
0
\$\begingroup\$

You could create a public class of type your script name and then just use this variable and .enabled = false.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Went with the above, thanks for your reply though :) \$\endgroup\$
    – CH99
    Commented Aug 17, 2015 at 13:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .