Search Unity

How to disable/enable a script? "enabled is not a member of UnityEngine.Component"

Discussion in 'Scripting' started by QuinnWinters, Jan 12, 2014.

  1. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    I'm trying to disable a script on a gameObject. I've read answer after answer on countless forum threads and they all lead back to the same error, "enabled is not a member of UnityEngine.Component."

    The answer I keep getting is to use this format:

    Code (csharp):
    1. gameObject.GetComponent("ThirdPersonCamera").enabled = false;
    or

    Code (csharp):
    1. GetComponent("ThirdPersonCamera").enabled = false;
    Both return the same error, with or without the quotation marks around ThirdPersonCamera. My goal is just to make it so the camera can't be moved when the pause menu is activated. I've tried dozens of alternate solutions and ways of writing the line and they all either return the same error or produce different errors. What am I doing wrong?
     
    Last edited: Jan 12, 2014
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Don't use strings in GetComponent. That makes it return Component (which indeed does not have an enabled property) rather than the correct type.

    --Eric
     
  3. MDragon

    MDragon

    Joined:
    Dec 26, 2013
    Posts:
    329
    This quick answer may not be it, but are you using C#?

    Code (csharp):
    1. GetComponent<MyScript>().enabled = false;
    Actually, ignore what I said before, because I'll already assume you did this. From the comments of this answer, this is what you should do.

    Cast the component first on a variable, and THEN enable or disable it. You can't do it at once.

    Basically... (quick written pseudo code)
    Code (csharp):
    1.  
    2. var temp : gameObject = gameObject; // or whatever method you wish. Just an example. Feel free to even store it in a permanent variable or find it elsewhere
    3. // If you want to get the script directly, try to use a var type of "MonoBehaivour" for scripts... apparently
    4. gameObject.GetComponent("ThirdPersonCamera).enabled = false;
    5.  
    Edit: Nevermind. Follow Eric's suggestion.
     
  4. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    Using a variable with the type Monobehavior got rid of the error, but the script still doesn't get disabled. Here's the best I've been able to come up with that doesn't give an error:

    Code (csharp):
    1. var randomVariable : MonoBehaviour;
    2.  
    3. randomVariable = gameObject.GetComponent(ThirdPersonCamera);
    4. randomVariable.enabled = false;
    I also tried changing gameObject into a variable that's a camera (e.g. var myCamera : Camera) and it behaved the same way - no error but no disabling of the script.

    Edit: By the way I'm using javascript.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Are you putting that code in a function, which is actually getting called?

    --Eric
     
  6. MDragon

    MDragon

    Joined:
    Dec 26, 2013
    Posts:
    329
    Check the inspector just to make sure that it is enabled or disabled. You never know, you could miss it.
    Also, to check if that area of the code actually goes through, put something like "print("Disabled!")" (or Debug.log, whichever you prefer) right there.
     
  7. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    Yes I've got it in the OnGUI function. I tried putting it in the Update function as well with no luck. I also thought that since my character controller also has an effect on the camera (via rotate with the mouse) that maybe I needed to disable it as well but that didn't seem to help. Here's the whole pause script:

    Code (csharp):
    1. #pragma strict
    2.  
    3. var paused : boolean;
    4. var myString : String = "Mute";
    5. var Mute : boolean;
    6. var guiSkin : GUISkin;
    7. var icon : Texture2D;
    8. var frameStyle : GUIStyle;
    9. static var Dialog = false;  //Camera pause
    10. var randomVariable : MonoBehaviour;
    11. var randomVariable2 : MonoBehaviour;
    12.  
    13. function Update (){
    14.  
    15.     //If escape key is pressed pause the game
    16.     if(Input.GetKeyDown("escape")){
    17.         paused = !paused;
    18.     }
    19.  
    20.     //If the game is paused stop everything that relies on time
    21.     if(paused){
    22.         Time.timeScale = 0;
    23.     }
    24.        
    25.     else{
    26.         Time.timeScale = 1;
    27.     }
    28.  
    29.     //If mute is true disable the sound
    30.     if (Mute == true){
    31.         gameObject.GetComponent(AudioListener).enabled = false;
    32.     }
    33.    
    34.     //If mute is not true enable the sound
    35.     else{
    36.         gameObject.GetComponent(AudioListener).enabled = true;
    37.     }
    38.  
    39. }
    40.  
    41. function OnGUI (){
    42.  
    43.     GUI.skin = guiSkin;
    44.  
    45.     //If the game is paused
    46.     if(paused){
    47.  
    48.         //Stop the camera
    49.         if(Dialog){
    50.             randomVariable = gameObject.GetComponent(ThirdPersonCamera);
    51.             randomVariable.enabled = false;
    52.             randomVariable2 = gameObject.GetComponent(ThirdPersonController);
    53.             randomVariable2.enabled = false;
    54.         }
    55.        
    56.         //Bring up the menu
    57.         GUI.Box (Rect (10,10, 100, 50), icon, frameStyle);
    58.  
    59.         //Create the first menu button
    60.         if (GUI.Button (Rect (Screen.width/2 - 100,Screen.height/2 - 240, 200, 100), "Menu")) {
    61.             Application.LoadLevel("Menu Level");
    62.             Time.timeScale = 1;
    63.         }
    64.  
    65.         //Create the second menu button
    66.         if (GUI.Button (Rect (Screen.width/2 - 100,Screen.height/2 - 120,200,100), "Restart")) {
    67.             Application.LoadLevel("Main Level");
    68.             Time.timeScale = 1;
    69.         }
    70.        
    71.         //Create the third menu button
    72.         if (GUI.Button (Rect (Screen.width/2 - 100,Screen.height/2,200,100), myString)) {
    73.            
    74.             //Disable sound if the mute button is pressed
    75.             if (myString == "Mute"){
    76.             myString = "Unmute";
    77.             Mute = true;
    78.             }
    79.            
    80.             //Enable sound if the mute button is pressed again
    81.             else{
    82.             myString = "Mute";
    83.             Mute = false;
    84.             }
    85.            
    86.         }
    87.        
    88.         //Create the fourth menu button
    89.         if(GUI.Button (Rect (Screen.width/2 - 100,Screen.height/2 + 120,200,100), "Quit")){
    90.             Application.Quit();
    91.             Time.timeScale = 1;
    92.         }
    93.        
    94.     }
    95.  
    96.     //If the game is not paused
    97.     else{
    98.    
    99.         if(!Dialog){
    100.             //Start camera
    101.         }
    102.        
    103.     }
    104.  
    105. }
    Edit in response to MDragon - I've checked the inspector during play and the script never gets disabled. I'll try the print method and see if it spits anything out.

    Edit: I found the problem. The variable "Dialog" in the script wasn't being set to true, hence not causing the code in question to execute. Using the Debug.log method helped me spot the problem. Thanks for the replies!
     
    Last edited: Jan 12, 2014