Search Unity

switch scripts on object during game

Discussion in 'Scripting' started by patrick1968, Oct 3, 2013.

  1. patrick1968

    patrick1968

    Joined:
    Oct 3, 2013
    Posts:
    2
    hey all,

    i am trying to switch my car script to hovercar script ...
    i have a car using wheels, and when i hit a key like R, i want to change the scripts. i mean take false somes and true the others.
    this is my script:

    Code (csharp):
    1.  
    2. var switchTarget = false;
    3.  
    4.  
    5. function Update () {
    6. if(switchTarget)
    7.  
    8.     {
    9.         GetComponent("StartGame").enabled = false;
    10.         GetComponent("SettingsMenu").enabled = false;
    11.         GetComponent("Axles").enabled = false;
    12.         GetComponent("Drivetrain").enabled = false;
    13.         GetComponent("CarDynamics").enabled = false;
    14.         GetComponent("SoundController").enabled = false;
    15.         GetComponent("AerodynamicResistance").enabled = false;
    16.         GetComponent("AxisCarController").enabled = false;
    17.         GetComponent("CarDamage").enabled = false;
    18.         GetComponent("Setup").enabled = false;
    19.         GetComponent("FuelTank").enabled = false;
    20.         GetComponent("Wheel").enabled = false;     
    21.         rigidbody.mass = 1;
    22.         GetComponent("CraftControl").enabled = true;
    23.         GetComponent("hover").enabled = true;
    24.     }
    25.  
    26. else
    27.  
    28.     {
    29.         GetComponent("StartGame").enabled = true;
    30.         GetComponent("SettingsMenu").enabled = true;
    31.         GetComponent("Axles").enabled = true;
    32.         GetComponent("Drivetrain").enabled = true;
    33.         GetComponent("CarDynamics").enabled = true;
    34.         GetComponent("SoundController").enabled = true;
    35.         GetComponent("AerodynamicResistance").enabled = true;
    36.         GetComponent("AxisCarController").enabled = true;
    37.         GetComponent("CarDamage").enabled = true;
    38.         GetComponent("Setup").enabled = true;
    39.         GetComponent("FuelTank").enabled = true;
    40.         GetComponent("Wheel").enabled = true;
    41.         rigidbody.mass = 1600;
    42.         GetComponent("CraftControl.js").enabled = false;
    43.         GetComponent("hover.js").enabled = false;
    44.     }
    45.  
    46. if(Input.GetKeyDown("r"))
    47. {
    48.         switchTarget = true;
    49. }
    50. }
    51.  

    script work sometimes on scene with only 2 script change but not with the complete scripts package.

    what am i doing wrong ... in short terms, i want to switch driving scripts for the flying scripts ...
    and sry for bad english
     
    Last edited: Oct 3, 2013
  2. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    What script is that code inside? also use the [ CODE ] blocks for posting code. Also you are only setting switchTarget to true... You should change the Input.GetKeyDown to Input.GetKeyUp and also add and if statement to actually flip the switch... like

    Code (csharp):
    1.  
    2. if ( Input.GetKeyUp("r"))
    3. {
    4. if ( switchTarget )
    5. switchTarget=false;
    6. else
    7. switchTarget=true;
    8. }
    9.  
     
  3. Marsupilami

    Marsupilami

    Joined:
    Sep 22, 2013
    Posts:
    21

    Personally I would change..

    Code (csharp):
    1.  
    2. if(Input.GetKeyDown("r"))
    3.  {
    4.  switchTarget = true;
    5.  }
    6.  
    to

    Code (csharp):
    1.  
    2. if(Input.GetKeyUp("r"))
    3.  {
    4.  switchTarget = !switchTarget;
    5.  }
    6.  
    This will flip the value of switchTarget each key press using the fewest lines of code.

    Also I would change the code like this. So you are only changing the mode when the key is pressed and not every frame that is rendered..

    Code (csharp):
    1.  
    2. var switchTarget = false;
    3.  
    4. function Update ()
    5. {
    6.   if(Input.GetKeyUp("r"))
    7.   {
    8.     switchTarget = !switchTarget;
    9.     if(switchTarget)
    10.     {
    11.       GetComponent("StartGame").enabled = false;
    12.       GetComponent("SettingsMenu").enabled = false;
    13.       GetComponent("Axles").enabled = false;
    14.       GetComponent("Drivetrain").enabled = false;
    15.       GetComponent("CarDynamics").enabled = false;
    16.       GetComponent("SoundController").enabled = false;
    17.       GetComponent("AerodynamicResistance").enabled = false;
    18.       GetComponent("AxisCarController").enabled = false;
    19.       GetComponent("CarDamage").enabled = false;
    20.       GetComponent("Setup").enabled = false;
    21.       GetComponent("FuelTank").enabled = false;
    22.       GetComponent("Wheel").enabled = false;
    23.       rigidbody.mass = 1;
    24.       GetComponent("CraftControl").enabled = true;
    25.       GetComponent("hover").enabled = true;
    26.     }
    27.     else
    28.     {
    29.       GetComponent("StartGame").enabled = true;
    30.       GetComponent("SettingsMenu").enabled = true;
    31.       GetComponent("Axles").enabled = true;
    32.       GetComponent("Drivetrain").enabled = true;
    33.       GetComponent("CarDynamics").enabled = true;
    34.       GetComponent("SoundController").enabled = true;
    35.       GetComponent("AerodynamicResistance").enabled = true;
    36.       GetComponent("AxisCarController").enabled = true;
    37.       GetComponent("CarDamage").enabled = true;
    38.       GetComponent("Setup").enabled = true;
    39.       GetComponent("FuelTank").enabled = true;
    40.       GetComponent("Wheel").enabled = true;
    41.       rigidbody.mass = 1600;
    42.       GetComponent("CraftControl").enabled = false;
    43.       GetComponent("hover").enabled = false;
    44.     }
    45.   }
    46. }
    47.  
    Also not sure what "StartGame" and "SettingsMenu" are doing, but do you really want these to be disabled during hovercraft mode?
     
    Last edited: Oct 3, 2013
  4. patrick1968

    patrick1968

    Joined:
    Oct 3, 2013
    Posts:
    2
    thank you for your answers.its not working at all, but i am surely doing something wrong. do all the scripts have to be in same folder ? and what is the best way to place my switch script: an empty game object with script attached or directly on game object ?
    should be better to use 2 objects, each one with own scripts, and disable 1 when using the 2 ?
     
    Last edited: Oct 3, 2013
unityunity