Search Unity

Enabling Components

Discussion in 'Scripting' started by grimmy, Mar 16, 2010.

  1. grimmy

    grimmy

    Joined:
    Feb 2, 2009
    Posts:
    396
    As far as I can tell it's only possible to enable/disable (activate/deactivate) gamneObjects through script but NOT components. Is this correct?

    If so , it seems odd that I can enable/disable components through the inspector at runtime.

    Anyone know?

    I should mention what I'm trying to do is toggle a full screen effect component on/off but instead the script just deactivate the gameobject which hold the effect (ie the camera)
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    6,907
    GameObject has .active, and many (though not all) components have .enabled which is equivalent. (The components which don't have that are, in theory, the ones which wouldn't make much sense to deactivate.)
     
  3. grimmy

    grimmy

    Joined:
    Feb 2, 2009
    Posts:
    396
    Yeah I tried that but I get ...

    VideoSettingsControllerScript.js(56,40): BCE0019: 'enabled' is not a member of 'UnityEngine.Component'.


    :(
     
  4. grimmy

    grimmy

    Joined:
    Feb 2, 2009
    Posts:
    396
    No you were right!

    Instead of
    Code (csharp):
    1.  
    2. var myCameraEffect:SSAOEffect;
    Iwas declaring my component as..
    Code (csharp):
    1.  
    2. var myCameraEffect:Component;
    3.  
    Now I can use enabled=true; freely.
    Many thanks!
     
  5. pandigital

    pandigital

    Joined:
    Mar 20, 2009
    Posts:
    15
    I have
    var color_script : SSAOEffect;
    color_script = GetComponent(ColorCorrectionEffect);
    color_script.enabled=false;

    I get error BCE0022: Cannot convert 'System.Type' to 'SSAOEffect'.


    if I try
    var color_script : SSAOEffect;
    function Update () {
    color_script = GetComponent(ColorCorrectionEffect);
    color_script.enabled=false;

    I get errorInvalidCastException: Cannot cast from source type to destination type.

    Any ideas ?