Click/touch object to enable/disable buttons inside Canvas

hello,

I want to make a simple switch to enable or disable UI buttons by clicking on any gameObject. (Using Unity 5.3.5).
I have multiple objects in my game view,and there are separate UI buttons inside the canvas for each of the object.
Whenever I click or touch on any object it will show the UI related to that object.

using the code bellow i am able to click only on Single object (there is also no touch input), i want to add multiple objects with UI switch…

How to write it inside this code… please help

using UnityEngine;
using System.Collections;
using UnityEngine .UI;
public class click1object : MonoBehaviour {


    void Start () {
   
    }
   

    void Update(){
        if (Input.GetMouseButtonDown(0)){ // if left button pressed...
            Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit)){
               
                print ("clicked");
           
            }
        }
    }
}

So, you want to activate some buttons when you click on an object in the scene and the buttons relate to that object? If the buttons are not always visible, just put them under an empty gameobject and grouped up based on what object they belong to. Then activate/deactivate that empty gameobject.

If the buttons are always visible, you might consider a canvas group
https://docs.unity3d.com/Manual/class-CanvasGroup.html
Which will allow you to make a group of object interactable or not by just toggling the canvas group

1 Like