- Home /
enabled is not a member of UnityEngine.Component
I wanna enable some components by their name in inspector and this is my code:
var Components : String[];
function OnEnable () {
for (var i= 0; i < Components.Length; i++){
GetComponent(Components[i]).enabled = true;
}
but I got this error:
enabled is not a member of UnityEngine.Component
Answer by fafase · May 21, 2015 at 10:46 AM
Component has no enabled member. It comes later in the hierarchy with Behaviour.
"enabled" always worked for me, for example if I have a component with the name "Light" and I write this: GetComponent(Light).enabled = true; It will work. but in here I wanna add the name of components from inspector and it doesn't work
Ok I realize you are using the string version which only returns Component and requires a cast to the type. In general, the string parameter should be avoided and I think they even removed it from unity 5
$$anonymous$$aybe, but I'm using unity4.6... or maybe it's a bug!
Finally found the sollution :) with this code, you can enable or disable any components that you want:
#pragma strict
var ComponentToActive : String[];
var ComponentToDeactive : String[];
////////////////////////////
function OnEnable () {
for (var i= 0; i < ComponentToActive.Length; i++){
(GetComponent(ComponentToActive[i]) as Behaviour).enabled = true;}
for (var j= 0; j < ComponentToDeactive.Length; j++){
(GetComponent(ComponentToDeactive[j]) as Behaviour).enabled = false;}
}
Your answer
Follow this Question
Related Questions
C# GetComponent, component not turning off. 2 Answers
How do I disable Script on FP Controller? (Solved) 2 Answers
Unable to get component from a gameobject 0 Answers
Unknown identifier when turning off SSAO through a JS script 1 Answer
How to Get TextMesh component from the Gameobject the script is attached to? 3 Answers