Hello
I have three box on scene and I want to detect which one is selected when user tapping screen.
Hello
I have three box on scene and I want to detect which one is selected when user tapping screen.
Below is a script I made for simple interactions, it uses an event in the inspector to handle what to do on click. Basically it works like the default button script, except for scene objects instead of UI. I’ve always just used the buttons cript without issues, but suddenly that doesn’t work anymore.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class RPS_Interaction : MonoBehaviour
{
public UnityEvent action;
void Update(){
if (Input.GetMouseButtonDown(0)){
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, Mathf.Infinity)){
GameObject obj = hit.collider.gameObject;
obj.GetComponent<RPS_Interaction>().action.Invoke();
}
}
}
}
I had an issue with the events of any object with the script firing when any other object is clicked, so I had to add the get component there to make sure it is only affecting the clicked object, I feel like there should be a way to do this in the condition instead, but this worked for me. Also I know it’s referring to mouse button, but I tested on iPad and it works with tap, haven’t been able to test the script in hololens yet though.