salahzar

ObjectAsButton

Apr 11th, 2021
436
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×