Search Unity

What's wrong with this code?

Discussion in 'Scripting' started by realm_1, Jun 4, 2011.

  1. realm_1

    realm_1

    Joined:
    May 18, 2011
    Posts:
    216
    Hi

    I try to dim some lights but it does not work.

    Here's my code:

    Code (csharp):
    1.  
    2.  
    3. var lightmath : float = 0;
    4. var lightlimit : float = 0;
    5. var islighting : boolean = false;
    6. var waittingtime : float = 0;
    7.  
    8. function FixedUpdate () {
    9.     if(!islighting) {
    10.         lightfgcver();
    11.     }
    12. }
    13.  
    14. function lightfgcver() {
    15.     islightning = true;
    16.     if(light.intensity>=lightlimit) {
    17.         light.intensity -= lightmath;
    18.     }else if(light.intensity<=0) {
    19.         light.intensity += lightmath;
    20.     }
    21.     yield WaitForSeconds(waittingtime);
    22.     islightning = false;
    23. }
    24.  
    And yeah I set the variables in the inspector.

    Does anybody have an idea?

    Thanks.

    realm_1
     
    Last edited: Jun 4, 2011
  2. jonbonazza

    jonbonazza

    Joined:
    Nov 6, 2010
    Posts:
    453
    1) It should be in the Update function, not FixedUpdate.
    2) What happens if light.intesity is between 0 and light limit (which I would expect it is most of the time).
     
  3. realm_1

    realm_1

    Joined:
    May 18, 2011
    Posts:
    216
    Oh yeah right I didn't see that.

    I solved the problem with this code:
    Code (csharp):
    1.  
    2.  
    3. var lightmath : float = 0;
    4. var lightlimit : float = 0;
    5. var islighting : boolean = false;
    6. var waittingtime : float = 0;
    7.  
    8. function FixedUpdate () {
    9.     if(!islighting) {
    10.         lightfgcver();
    11.     }
    12. }
    13.  
    14. function lightfgcver() {
    15.     islightning = true;
    16.     if(light.intensity>=lightlimit) {
    17.         lightmathfgcver = -lightmath;
    18.     }else if(light.intensity<=0) {
    19.         lightmathfgcver = -lightmath;
    20.     }
    21.     light.intensity += lightmath;
    22.     yield WaitForSeconds(waittingtime);
    23.     islightning = false;
    24. }
    25.  
    And the start light intensity is 0.1.
    And FixedUpdate is ok because I want to dim the light constant.

    Thanks.

    realm_1