Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

UI Button with 2 events

Discussion in 'UGUI & TextMesh Pro' started by Tdonk, Mar 6, 2015.

  1. Tdonk

    Tdonk

    Joined:
    Jan 29, 2015
    Posts:
    7
    Hi,
    I want to create a button for hide/unhide some object. I created the button and after i added in OnClick () tab MeshRender.enable (unchecked). When I click the button my object disappear.
    I would like to unhide the same object with another click in the same button but............ I can't find the solution.

    Exemple:
    - first click = MeshRender.enable = false (change the button color from "normal color" to "pressed color" and hold the color)

    - second click = MeshRender.enable = true (change the button color from "pressed color" to "normal color" and hold the color)

    Thank you for your help
     
  2. Ramcat

    Ramcat

    Joined:
    Aug 16, 2014
    Posts:
    95
    Store your own state.

    Code (CSharp):
    1. private bool _AlreadyClicked = false;
    2.     public void Click()
    3.     {
    4.         // Has the user already clicked?
    5.         if (_AlreadyClicked)
    6.         {
    7.             // Yes
    8.             _AlreadyClicked = false;
    9.  
    10.             // Show object
    11.         }
    12.         else
    13.         {
    14.             // No -- first click
    15.             _AlreadyClicked = true;
    16.  
    17.             // Hide object
    18.         }
    19.     }
     
    Tdonk likes this.
  3. iivo_k

    iivo_k

    Joined:
    Jan 28, 2013
    Posts:
    314
    Or just do
    Code (CSharp):
    1. gameObject.setActive(!gameObject.activeSelf);
     
    Deleted User and Tdonk like this.
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Alternately, if for some reason you wanted to avoid writing any script whatsoever, you could have two identical-looking buttons in the same position, the first of which disables your mesh renderer, sets itself to inactive, and sets the other button to active, and the second of which does the reverse. So the second click (in the same location) is actually triggering a different button.

    In fact, you could also use two non-identical buttons, and then you'll be able to tell the current state based on which button is visible.
     
    Last edited: Mar 7, 2015
    andrewjason and Tdonk like this.
  5. Tdonk

    Tdonk

    Joined:
    Jan 29, 2015
    Posts:
    7
    @Antistone Thank you for your help. I used this solution in many project. In this way work very well but I must create 2 button. I want to create something from script and put everything in the same button.

    @Ramcat


    Can you make an example with some gameobject? I tried many version of this script but don't work. Thanks
     
  6. Ramcat

    Ramcat

    Joined:
    Aug 16, 2014
    Posts:
    95
    Actually @iivo_k 's solution is better than mine. But here is the code I did with his solution.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GMScript : MonoBehaviour
    5. {
    6.     public GameObject _Cube;
    7.  
    8.     public void Click()
    9.     {
    10.         _Cube.SetActive(!_Cube.activeSelf);
    11.     }
    12. }
    So I used a button in the new GUI system and wired it's click event to the "Click" method shown above. The _Cube game object was filled using the Editor hole - assign a game object from the hierarchy to that slot in the Inspector tab/window. But could be supplied via code as well.

    First click hides the object, second click shows it, and so on.
     
    Tdonk likes this.