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): if(Input.GetButtonDown("Light")) { y += 1; Debug.Log(y); if(y == 1) { light.SetActive(true); on.audio.Play(); isOn = true; isOff = false; } if(y == 2) { light.SetActive(false); off.audio.Play(); y = 0; isOff = true; isOn = false; } } if(isOn = true) { snow.particleSystem.startColor = (new Color(255,255,255)); } if (isOff = true) { snow.particleSystem.startColor = (new Color(0,0,0)); } 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?
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): if(isOn) { snow.particleSystem.startColor = (new Color(255,255,255)); } if (isOff) { snow.particleSystem.startColor = (new Color(0,0,0)); }
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.
I love to teach. You can get away with using one bool if you (eg) Code (csharp): if(isOn) { //Cut the grass } else { //Wash the car lol }
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