Hi. I am having a problem with a toggle component inside of Unity and C#. Here is my code: //option_AntiAliasing Start public bool option_AntiAliasing = false; public void optionAntiAliasing(){ if(option_AntiAliasing = true){ QualitySettings.antiAliasing = 4; } else if(option_AntiAliasing = false){ QualitySettings.antiAliasing = 0; } } //option_AntiAliasing End The top half of the void optionAntiAliasing works. (turning Anti-Aliasing on) but the bottom half doesn't. I don't think that it is a problem with the code as I have played around with it and it seems to be that the toggle button knows when it is enabled but not when it is disabled. Any help would be greatly appreciated.
As ThermalFusion said, you're using a single equals sign, which is used to ASSIGN a variable. Two equals signs are used to COMPARE a variable. Code (csharp): //option_AntiAliasing Start public bool option_AntiAliasing = false; public void optionAntiAliasing(){ if(option_AntiAliasing == true){ QualitySettings.antiAliasing = 4; }else if(option_AntiAliasing == false){ QualitySettings.antiAliasing = 0; } } //option_AntiAliasing End Also, just for future reference, if you paste your script here in the forum, wrap it in CODE tags and it will appear all nice and formatted. Type CODE in these brackets [ ] at the start, and then /CODE in these brackets at the end [ ]. Good luck!
I am sorry to say this but it hasn't worked. With the two == it doesn't work and with one = it does. However it still only turns on Anti-Aliasing not off again. Code (csharp): using UnityEngine; using System.Collections; public class Options_Menu : MonoBehaviour { //option_AntiAliasing Start public bool option_AntiAliasing = false; public void optionAntiAliasing(){ if(option_AntiAliasing == true){ QualitySettings.antiAliasing = 4; }else if(option_AntiAliasing == false){ QualitySettings.antiAliasing = 0; } } //option_AntiAliasing End }
This assumes you are running optionAntiAliasing() and changing option_AntiAliasing separately which isn't a very good way to structure this. Instead, require the input up front when the method is run so you're forced to provide correct input. Code (csharp): using UnityEngine; using System.Collections; public class Options_Menu : MonoBehaviour { public void optionAntiAliasing(bool toggle) { if (toggle) QualitySettings.antiAliasing = 4; else QualitySettings.antiAliasing = 0; } } There is no need to 'else if' on a bool if it is the only condition. You could also do this... Code (csharp): public class Options_Menu : MonoBehaviour { public void optionAntiAliasing(bool toggle) { QualitySettings.antiAliasing = toggle ? 4 : 0; } }
Thanks for you quick answer however it doesn't appear to be working. I re-entered the code and the functions to the correct places but it still isn't working.