- Home /
'enabled' is not a member of UnityEngine.Component
I was working on a project before, saved it, and when I went to compile it I got this error. The line it is having a problem with is:
script.enabled = false;
Every other time I ran it before, I never got this error. Nothing has changed from 2 days ago to now.
The full code this line is in is (it's a menu named "Dead," a menu shown for player death):
var newSkin : GUISkin;
var logoTexture : Texture2D;
function theFirstMenu() {
//layout start
GUI.BeginGroup(Rect(Screen.width / 2 - 150, 50, 300, 200));
//the menu background box
GUI.Box(Rect(0, 0, 300, 200), "You died in the pit. You were *just* there.");
//logo picture
GUI.Label(Rect(15, 10, 300, 68), logoTexture);
///////main menu buttons
//game start button
if(GUI.Button(Rect(55, 50, 180, 40), "Start Over"))
{
var script = GetComponent("Dead");
Application.LoadLevel("gameFINAL");
script.enabled = false;
}
//quit button
if(GUI.Button(Rect(55, 150, 180, 40), "Main Menu"))
{
Application.LoadLevel("titleFINAL");
}
//layout end
GUI.EndGroup();
}
function OnGUI () {
//load GUI skin
GUI.skin = newSkin;
//execute theFirstMenu function
theFirstMenu();
}
Hoping to resolve this error with the line. Thanks!
I don't know what your component named "Dead" is but it obviously does't have a .enabled property....
It's the name of the script. I should have made that clear.
Right. I assumed as much. Your Dead script doesn't have an enabled property
What code would I use, or where would I go to research on to implement that? Noob to Unity even now.
@perchik: no, all scripts (that can be attached to GameObjects anyway) have an enabled property, because they inherit from $$anonymous$$onoBehaviour, which inherits from Behaviour, which does have an enabled property.
Answer by Eric5h5 · Jul 30, 2013 at 09:56 PM
Don't use quotes in GetComponent, that makes it return Component (which does not have an enabled property as you can see in the docs) instead of your script type.
Answer by hoy_smallfry · Jul 30, 2013 at 10:08 PM
The error you are getting is popping up probably because you've added #pragma strict
somewhere at the top, which enforces certain checks to happen.
According to the Unity documentation, GetComponent
returns a Component
type. If a string is used as the parameter, there is no hard script type to deduce the return type from, so it just returns a Component
.
By looking at the list of members that `Component` has, you can see that there is no enabled
member. If you are familiar with the concept of inheritance and polymorphism, you'll know that your Dead
script has an enabled
member because that member is inherited from `Behaviour`. But since you are receiving the dead script as a Component
instead of a Dead
type, which is earlier along the heirarchy, you cannot access it's enabled
member while it's perceived as a Component
.
You should explicitly state that var script
is of type Dead
by using the actual type instead of a string. This will make sure it returns a Dead
type, like so:
var script = GetComponent(Dead);