Search Unity

Activate variable componennt

Discussion in 'Scripting' started by bumble864, May 30, 2011.

  1. bumble864

    bumble864

    Joined:
    Jan 27, 2011
    Posts:
    128
    Hi,
    I´m trying to write a generic script for turning components on and off depending on if the player can do action X or not.
    I´m starting basic and just making it based on time from start. This is what I have right now.
    If I use the variable it gives me this error "Assets/activate script.js(6,13): BCE0023: No appropriate version of 'UnityEngine.Component.GetComponent' for the argument list '(UnityEngine.Component)' was found.". But if I type the name of the component it works just fine.

    Is there a way to use a variable component so I can use this same script in multiple places?
     
  2. fox

    fox

    Joined:
    Jan 14, 2009
    Posts:
    118
    not sure you're making this the best way, but to help you out right now:

    Code (csharp):
    1. var test:Component;
    2. var time:int;
    3.  
    4. function Start () {
    5. yield WaitForSeconds(time);
    6. GetComponent(test.GetType()).enabled = false;
    7. }
    note that I added "GetType()" after your component, and changed from active to enabled.
     
  3. bumble864

    bumble864

    Joined:
    Jan 27, 2011
    Posts:
    128
    Thanks fox, I retyped active and not enable so that was my mistake... Your script worked perfectly though. Thanks

    One example of where I am putting this is when the player comes on screen. I don´t the player to have control right away until the avatar has done their entrance. This way the player has no control until the script is enabled.