Search Unity

Bools acting strangely?

Discussion in 'Scripting' started by jmane2012, Nov 28, 2012.

  1. jmane2012

    jmane2012

    Joined:
    Apr 17, 2012
    Posts:
    69
    While trying to program a particle system of snow I noticed a very weird bug. First I will provide an example then explain (easier that way).

    Code (csharp):
    1. if(Input.GetButtonDown("Light"))
    2.         {
    3.             y += 1;
    4.             Debug.Log(y);
    5.             if(y == 1)
    6.             {
    7.                 light.SetActive(true);
    8.                 on.audio.Play();
    9.                 isOn = true;
    10.                 isOff = false;
    11.             }
    12.            
    13.             if(y == 2)
    14.             {
    15.                 light.SetActive(false);
    16.                 off.audio.Play();
    17.                 y = 0;
    18.                 isOff = true;
    19.                 isOn = false;
    20.             }
    21.         }
    22.        
    23.         if(isOn = true)
    24.         {
    25.             snow.particleSystem.startColor = (new Color(255,255,255)); 
    26.         }
    27.        
    28.         if (isOff = true)
    29.         {
    30.             snow.particleSystem.startColor = (new Color(0,0,0));   
    31.         }
    Before starting the game, the bools are correct, on is false and off is true. When I start, it changes them both to true. I have tried declaring the bools to be how they are supposed to be in the start method, but that didn't work. Any suggestions?
     
  2. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    You're using assignment in your if statements (i.e. one equals sign =). They should be comparisons (i.e. double equals ==).

    EDIT: or even simpler in this case, just:

    Code (csharp):
    1.  
    2. if(isOn)
    3.  
    4.         {
    5.  
    6.             snow.particleSystem.startColor = (new Color(255,255,255));  
    7.  
    8.         }
    9.  
    10.        
    11.  
    12.         if (isOff)
    13.  
    14.         {
    15.  
    16.             snow.particleSystem.startColor = (new Color(0,0,0));    
    17.  
    18.         }
    19.  
     
    Last edited: Nov 29, 2012
  3. jmane2012

    jmane2012

    Joined:
    Apr 17, 2012
    Posts:
    69
    You know, I didn't expect that to work, but it did! Thanks man, first attempt at actually making things without a tutorial, doin good so far.
     
  4. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    Excellent - keep it up :)
     
  5. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    I love to teach. You can get away with using one bool if you (eg)

    Code (csharp):
    1.  
    2. if(isOn) {
    3.   //Cut the grass
    4. }
    5. else {
    6.   //Wash the car lol
    7. }
    8.  
     
  6. jmane2012

    jmane2012

    Joined:
    Apr 17, 2012
    Posts:
    69
    Yeah I originally did use an if/else statement, but since i was using isOn = etc it wasn't working so I changed it. Probably gonna switch back now