Search Unity

trouble with toggling light on and off

Discussion in 'Scripting' started by sinkler747, Dec 21, 2012.

  1. sinkler747

    sinkler747

    Joined:
    Dec 10, 2012
    Posts:
    9
    I am working on a script where i have a spot light attached to a flashlight object, and i want to cut it on or off with the " o " button. i tried several different ways, until i searched the forums and found a method that i thought would work.( 1st commented block of code) the problem with that block of code is that it turns on or off all lights, and i just want to be able to toggle only the flashlight. i got it to work by using 2 different letters (o and p, as in the 2nd commented block of code, but that is not how i want it to work. anyway i tried a combination of the 2 commented blocks of code and i don't get any errors, but when i start the scene, the light is on, then when i press o the light goes off. but, when i press o again to turn it on, nothing happens except the console says " the light is off" which tells me that it is not returning to the start of the code... i am stuck now.... below is the code and the several other attempts that i tried. i'm still learning with difficulty........
    thanks

    Code (csharp):
    1. // public var lightOnOff = light.intensity;
    2.  
    3. public var lightOnOff : boolean = true;
    4.  
    5. function OnGUI()
    6. {
    7.      GUI.Label (Rect (10, 10, 200, 20), "Use o to turn flashlight on or off");
    8. }
    9.  
    10. function Update()
    11. {
    12.     if(Input.GetKeyDown("o"))
    13.     {
    14.         if(lightOnOff == false)
    15.         {
    16.             light.intensity = lightOn;
    17.             Debug.Log("Light is on");
    18.            
    19.         }
    20.         else
    21.             light.intensity = lightOff;
    22.             Debug.Log("light is off");
    23.     }
    24. }
    25.  
    26. /*
    27.  
    28. function Update () {
    29.  
    30.  
    31.  
    32. if(Input.GetKeyDown("o"))
    33.  
    34. {
    35.  
    36.  if(GetComponent(Light).enabled == false)
    37.  
    38.  {  
    39.  
    40.    GetComponent(Light).enabled = true;
    41.  
    42.    Debug.Log("light on");
    43.  
    44.   }
    45.  
    46. else
    47.  
    48.    GetComponent(Light).enabled = false;
    49.  
    50.      Debug.Log("light off");
    51.  
    52. }
    53.  
    54. }
    55.  
    56. */
    57.  
    58.  
    59. /*
    60. function Update()
    61. {
    62.  
    63.  
    64.     if (Input.GetKeyDown("o"))
    65.     {
    66.         light.intensity = lightOn;
    67.     }
    68.         if(Input.GetKeyDown("p"))
    69.         {
    70.             light.intensity = lightOff;
    71.         }
    72. }
    73.  
    74. */
    75.  
    76.  
    77. /*
    78. function Update ()
    79. {
    80.     if (Input.GetKeyDown("o"))  (lightOff == true)
    81.     {
    82.         light.intensity = lightOn;
    83.         //lightOn;
    84.     }
    85. }
    86.  
    87. */
    88.  
    89. /* 
    90.     if(lightOnOff == 0)
    91.     {
    92.         if (Input.GetKeyDown("o"))
    93.         {
    94.             lightOn;
    95.         }
    96.         else
    97.         {
    98.             lightOff;
    99.         }
    100. */
    101.  
    102.  
    103.  
    104.  
    105. /* 
    106.     if(Input.GetKeyDown("o"))
    107.         {
    108.             light.intensity = lightOn;
    109.         }
    110.             if(Input.GetKeyDown("o"))
    111.             {
    112.                 light.intensity = lightOff;
    113.             }
    114. */
    115.  
     
  2. UnityCoder

    UnityCoder

    Joined:
    Dec 8, 2011
    Posts:
    534
    After

    Code (csharp):
    1. if(Input.GetKeyDown("o"))    {
    2.  
    add below line and check this out.

    Code (csharp):
    1. lightOnOff =! lightOnOff;
     
  3. sinkler747

    sinkler747

    Joined:
    Dec 10, 2012
    Posts:
    9
    awesome UnityCoder, I would have never thought about that. thanks