Search Unity

Problem with toggle component

Discussion in 'Scripting' started by Jay cloth, Nov 29, 2015.

  1. Jay cloth

    Jay cloth

    Joined:
    May 27, 2015
    Posts:
    12
    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.
     

    Attached Files:

  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Please use code tags.
    You need == in your if statements.
     
  3. Comafly

    Comafly

    Joined:
    May 30, 2014
    Posts:
    87
    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):
    1.  
    2. //option_AntiAliasing Start
    3. public bool option_AntiAliasing = false;
    4.  
    5. public void optionAntiAliasing(){
    6.    if(option_AntiAliasing == true){
    7.       QualitySettings.antiAliasing = 4;
    8.    }else if(option_AntiAliasing == false){
    9.       QualitySettings.antiAliasing = 0;
    10.    }
    11. }
    12. //option_AntiAliasing End
    13.  
    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!
     
    ThermalFusion likes this.
  4. Jay cloth

    Jay cloth

    Joined:
    May 27, 2015
    Posts:
    12
    Thank You. I am new to the software and c#.
     
  5. Jay cloth

    Jay cloth

    Joined:
    May 27, 2015
    Posts:
    12
    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):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Options_Menu : MonoBehaviour {
    6.  
    7.     //option_AntiAliasing Start
    8.     public bool option_AntiAliasing = false;
    9.    
    10.     public void optionAntiAliasing(){
    11.         if(option_AntiAliasing == true){
    12.             QualitySettings.antiAliasing = 4;
    13.         }else if(option_AntiAliasing == false){
    14.             QualitySettings.antiAliasing = 0;
    15.         }
    16.     }
    17.     //option_AntiAliasing End
    18. }
    19.  
     
  6. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,537
    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):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Options_Menu : MonoBehaviour {
    5.     public void optionAntiAliasing(bool toggle) {
    6.         if (toggle) QualitySettings.antiAliasing = 4;
    7.         else QualitySettings.antiAliasing = 0;
    8.     }
    9. }
    10.  
    There is no need to 'else if' on a bool if it is the only condition.

    You could also do this...

    Code (csharp):
    1. public class Options_Menu : MonoBehaviour {
    2.     public void optionAntiAliasing(bool toggle) {
    3.         QualitySettings.antiAliasing = toggle ? 4 : 0;
    4.     }
    5. }
     
  7. Jay cloth

    Jay cloth

    Joined:
    May 27, 2015
    Posts:
    12
    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.