Search Unity

How do I make an image become visible when two toggles are enabled

Discussion in 'Scripting' started by PapaEdgy, May 26, 2019.

  1. PapaEdgy

    PapaEdgy

    Joined:
    Dec 25, 2018
    Posts:
    1
    I'm trying to make an image become visible when two toggle (game objects) are enabled. How could I do this?
    .
    I'm somewhat new to programming as well, so any help will be great. Thanks!
     
  2. Tarball

    Tarball

    Joined:
    Oct 16, 2016
    Posts:
    142
    Code (CSharp):
    1. if (toggle1.isOn && toggle2.isOn)
    2.      gameobject.setActive(true);
    3. else gameobject.setActive(false);
    That's untested, but you get the idea. It should work.
     
  3. Grawnya

    Grawnya

    Joined:
    Apr 5, 2018
    Posts:
    9
  4. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    789
    To add to this:

    Toggles in Unity have an event in the inspector for OnValueChanged. Assign a method to each toggle, LeftToggle and RightToggle for example and when changed, set a boolean to the toggles value. No need to check in update or the like, only check when a toggles value changes.

    https://docs.unity3d.com/ScriptReference/UI.Toggle-onValueChanged.html