Advertisement
salahzar

ObjectAsButton

Apr 11th, 2021
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.78 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5.  
  6. public class ObjectAsButton : MonoBehaviour
  7. {
  8.  
  9.     public GameObject definedButton;
  10.     public UnityEvent OnClick = new UnityEvent();
  11.  
  12.     // Use this for initialization
  13.     void Start()
  14.     {
  15.         definedButton = this.gameObject;
  16.     }
  17.  
  18.     // Update is called once per frame
  19.     void Update()
  20.     {
  21.         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  22.         RaycastHit Hit;
  23.  
  24.         if (Input.GetMouseButtonDown(0))
  25.         {
  26.             if (Physics.Raycast(ray, out Hit) && Hit.collider.gameObject == gameObject)
  27.             {
  28.                 Debug.Log("Button Clicked");
  29.                 OnClick.Invoke();
  30.             }
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement