Sign up ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 7 down vote accepted

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.

share|improve this answer
    
Perfect! Thanks :) – CH99 Aug 17 at 13:14
3  
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. – o0'. Aug 17 at 19:41

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

share|improve this answer
1  
Went with the above, thanks for your reply though :) – CH99 Aug 17 at 13:15

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!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.