Search Unity

Question how to check if ui toggle is true or false?

Discussion in 'Scripting' started by Aviation_Simmer, Apr 15, 2022.

  1. Aviation_Simmer

    Aviation_Simmer

    Joined:
    Aug 30, 2021
    Posts:
    110
    hi! I want to read if the UI toggle is true or false. I have this script which doesn't work for no reason
    Any Idea how to check that? Thanks!

    Code (CSharp):
    1.             if (toggleAP == true)
    2.             {
    3.                 AutoPilot = true;
    4.             }
    5.             else
    6.             {
    7.                 AutoPilot = false;
    8.             }
     
    Last edited: Apr 15, 2022
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Please just post more of the relevant code to reduce guesswork.
    If toggleAP is anything but a bool, the code makes no sense. But if it's a bool, then what is the ui toggle?

    If toggleAP is a Toggle (https://docs.unity3d.com/2017.3/Documentation/ScriptReference/UI.Toggle.html) then you get its value through the isOn property. Thus you need to compare that, not to the component itself.

    It is also unnecessary to compare booleans to true or false. (someBool == true) is the same as just (someBool), and (someBool == false) is the same as just (!someBool). And if the above is all the if statement is supposed to do you could even reduce the entire code to someBool = anotherBool. Or if they are always identitcal anyways, just use the ui bool and delete the other.
     
    russisunni and MelvMay like this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    This doesn't describe a problem and your script is about as minimal as it's possible to post; it's just "AutoPilot = toggleAP" with no description of what either of them are.

    I think you need to try to spend more time describing what it is you're showing as well as describing what "doesn't work" means in real terms.

    Note that the dedicated UI forum is here.
     
  4. Aviation_Simmer

    Aviation_Simmer

    Joined:
    Aug 30, 2021
    Posts:
    110
    Sorry.. I am not good at explaining things. So basicly what I mean is: I have a UI toggle. I want to read in the code if the toggle is true or not. (from my research and what I genneraly know: if (examplename == true))
    Code (CSharp):
    1.     public bool AutoPilot = false;
    2.     public Toggle toggleAP;
    3.  
    4.     void Update()
    5.     {
    6.         _AutoPilot(); //have the AutoPilot in a Update function to work at every frame.
    7.         SetVerticalSpeed(); //have the setVerticalSpeed in a Update function to work at every frame.
    8.         SetHeading(); //have the setHeading in a Update function to work at every frame.
    9.         SetAutopilot(); //have the setAutopilot in a Update function to work at every frame.
    10.     }
    11.  
    12.     void SetAutopilot()
    13.     {
    14.         if (toggleAP == true)
    15.         {
    16.             AutoPilot = true;
    17.         }
    18.         else
    19.         {
    20.             AutoPilot = false;
    21.         }
    22.     }
    or... if i say it like that: if (the toggle is checked) now what code is that (the toggle is checked)? I thought it is if (toggle == true)...
     
    Last edited: Apr 15, 2022
  5. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    MelvMay likes this.