BCE0019: 'enabled' is not a member of 'UnityEngine.Component'. (70690)

Hi,

I get this error when trying to enable/disable a script:
BCE0019: ‘enabled’ is not a member of ‘UnityEngine.Component’.

I know that this is because I am trying to use #Pragma Strict but cannot work out that right way to do it.

Here is an example code:

#pragma strict
var movementScript : Component; //I drag and drop the selected script in the inspector

function Start () {
	movementScript.enabled = true;
}

It seems like such a simple problem but I just can’t seem to work it out.

Thanks!

Paul

4 Answers

4

Component has no .enabled property. Instead of that, use the name of your script.

I had no idea you could setup a drag and drop component variable that way Eric5h5! Thank you once again!

–

Can you also explain how I can take a similar approach with this please Eric5h5? var controllers = new List.<Component>();

–

I'm not quite sure what you're asking, though it might be better if you started a new question for that.

–

No problem Eric5h5, will do! Thanks.

–

// Edit: This is for accessing scripts attached to other GameObjects //

If you get it from a GameObject, the .enabled works:

myGameobjectName.GetComponent(myScriptName).enabled = true;

So at the top, you can use

var myGameobjectName : GameObject;

and if you want to set it without needing to use the Inspector, use this in your Start () function:

myGameobjectName = GameObject.Find(“actual_name_of_gameobject_in_hierarchy”);

So, myGameObjectName is now a variable which holds a reference to the GameObject. (Do the “Find” in Start() not Update() because it’s expensive.) Then you can use myGameObjectName.GetComponent(name_of_component) to access scripts or other components attached to that GameObject.

For example, to change the variable “power” in your script, use:

myGameobjectName.GetComponent(myScriptName).power = 74;

Or to see if a Boolean (like playerAlive) in the script is currently true or false, use:

if (myGameobjectName.GetComponent(myScriptName).playerAlive == false) {
start_funeral();
}

// sorry so morbid… :wink:

You misunderstand; .enabled is not a member of Component, and it doesn't have anything to do with "getting it from a GameObject". What you're doing isn't related to the question since you're not using Component. If you used quotes in GetComponent in order to make it return Component instead of the type, then .enabled would not work.

–

var script : movementScript;

function Start() 
{
movementScript = GetComponent(movementScript); 
movementScript.enabled = false;
}

In school I have this problem and I can’t seem to get it to work again. I imported a asset and it said I had something wrong with the mouse look script, so I deleted the imported asset and it still said:

Assets/Standard Assets/Character Controllers/Sources/Scripts/MouseLook.cs(61,35): error CS1061: Type UnityEngine.Component' does not contain a definition for freezeRotation’ and no extension method freezeRotation' of type UnityEngine.Component’ could be found (are you missing a using directive or an assembly reference?)