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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to disable button on on click?

Discussion in 'Input System' started by usernotauser, Sep 22, 2022.

  1. usernotauser

    usernotauser

    Joined:
    Nov 6, 2021
    Posts:
    12
    I want to show image using OnMouseDown on a game object in the scene. I wrote the script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ImageClick : MonoBehaviour
    7. {
    8.     [SerializeField]
    9.     public Image image;
    10.     public Button closingButton;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         image.enabled = false;
    16.         closingButton.enabled = false;
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void OnMouseDown()
    21.     {
    22.  
    23.         image.enabled = !image.enabled;
    24.         closingButton.enabled = !closingButton.enabled;
    25.     }
    26.  
    27.     void Update()
    28.     {
    29.         if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.Escape)) //closing image on RMB click or on ESC key press
    30.         {
    31.             image.enabled = false;
    32.             closingButton.enabled = false;
    33.         }
    34.        
    35.     }
    36.  
    37. }
    38.  
    which I attach to the game object in the scene. I also added the Button which is meant to appear with the appearance of image and disappear on click. I tried using the OnClick method in the Button's Inspector pane:

    but it does not work correctly: the button appears on start, closes the image, but does not disables itself after click.
    What can be done to make it work like intended?
    Thank you in advance
     
  2. usernotauser

    usernotauser

    Joined:
    Nov 6, 2021
    Posts:
    12
    Okay, i fixed it. Maybe not the best way to fix it, but it works.
    Here's the new script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ImageClick : MonoBehaviour
    7. {
    8.     [SerializeField]
    9.     public Image image;
    10.     public GameObject closingButton;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         image.enabled = false;
    16.         closingButton.SetActive(false);
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void OnMouseDown()
    21.     {
    22.  
    23.         image.enabled = !image.enabled;
    24.         closingButton.SetActive(true);
    25.     }
    26.  
    27.     void Update()
    28.     {
    29.         if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.Escape)) //closing image on RMB click or on ESC key press
    30.         {
    31.             image.enabled = false;
    32.             closingButton.SetActive(false);
    33.         }
    34.      
    35.     }
    And OnClick for the button on the Inspector pane I selected GameObject.SetActive for the button