- Home /
BCE0019: 'enabled' is not a member of 'UnityEngine.Component' when using List
Hi,
I get this error when trying to enable/disable a script: BCE0019: 'enabled' is not a member of 'UnityEngine.Component'. The script I am trying to target is chosen from a List array.
I have learned that 'Component' does not have the function 'enabled' and I would normally use the name of the script itself to correct the problem as I am using #pragma strict. However I am having problems working out how to declare the script name when using a list array.
Here is some example code that works without pragma strict, but fails with it.
#pragma strict
import System.Collections.Generic;
var addComponentsHere = new List.<Component>();
function Start () {
addComponentsHere[0].enabled = true;
addComponentsHere[1].enabled = false;
addComponentsHere[2].enabled = true;
}
Thanks!
Paul
Thanks robertu! That does indeed work. So should I just use lists using monobehaviour? The duality of both ways working is confusing me.
Also in further testing, it does not work if I try to change a value that is not part of monobehaviour. For example:
addComponentsHere[0].Controller = controllerType;
Which gives the error 'Controller' is not a member of 'UnityEngine.$$anonymous$$onoBehaviour'.
A quick check says the class hierarchy is:
Object->Component->Behaviour->$$anonymous$$onoBehaviour
and 'enabled' actually comes form Behaviour not $$anonymous$$onoBehaviour. So you could have a list of any one of these and the assignment (to the list) would work. It would have to be Behaviour or $$anonymous$$onoBehaviour for you to access 'enabled'. $$anonymous$$onoBehaviour makes the most sense to me for what you may be doing.
Answer by robertbu · May 12, 2013 at 01:24 PM
'enabled' comes from Monobehaviour, so how about a list of Monobehavours.
O$$anonymous$$, I think I am getting the whole idea. So now I just get a warning: Implicit downcast from 'UnityEngine.Component' .... But it all works fine.
After searching my guess is to use: #pragma downcast after #pragma strcit to prevent the warning. If so does this reduce the efficiency of #pragma strict in any way?
I don't know from personal experience, but according to a quick Google search, #pragma downcast just removes the downcast warning. But I also read that this may not be the best fix for the warning.
But the pragma may not be the best solution. In the examples I read about, they were getting the warning because they were using he string version of GetComponent() rather than passing the type. That is they were doing:
GetComponent("$$anonymous$$yScript");
rather than:
GetComponent($$anonymous$$yScript);
This makes sense. In the former, the compiler does not know the type, so it must return a component. In the second case, the compiler will knows the type.
Hi roberbu, yeah I read that as well but it does not seem to fit my scenario. $$anonymous$$y guess is that it is O$$anonymous$$. Thanks for all your help!
Your answer
![](https://koobas.hobune.stream/wayback/20220613102055im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to Copy a Component if it contains List? 1 Answer
Removing a Component while in a List 1 Answer
.enabled Problems 1 Answer
How to read X and Y component ( of a vector2) from a list 1 Answer