- Home /
EventTrigger on 3D object not working ?
i want to make a 'Select' and 'Deselect' function on 3D object.. i'm using eventtrigger with ISelectHandler and IDeselectHandler, i'm add as component to 2 object: UI button and 3D object cube.. and the result, on UI its works but on 3D object its nothing happen..
here my script, please help thanks
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
namespace DebugScripts
{
public class SelectedObjectv2 : MonoBehaviour, ISelectHandler, IDeselectHandler
{
public void OnSelect (BaseEventData eventData)
{
print ("Object Selected");
}
public void OnDeselect (BaseEventData eventData)
{
print ("Object Deseleted");
}
}
}
You need at least colliders on your 3D objects to interact with them trough events. If adding colliders does not trigger the UI events then you need to use On$$anonymous$$ouseDown function ins$$anonymous$$d.
I already tried adding colliders but still not working.. I want to avoid using On$$anonymous$$ouseDown because my object is many, but thanks for respons
Answer by thesanketkale · Aug 28, 2018 at 12:53 PM
Hi @BudyDubby, I know this is an old thread but I am just posting an answer now as this question pops first on the google search for the same thing I faced and overcame.
Now Raycast would be a solution to this, but I feel it is more like an overkill for triggering events on 3D objects.
So to answer your question, you need to add a collider on your 3D object like a Box, sphere or Capsule according to the shape of your object. You may even add a combination of these colliders to have a complex compound of colliders to get event triggers on specific colliders for specific tasks.
So your script seems fine, once you add a collider, your 3D object should trigger corresponding events.
Hope it helps anyone who lands on this question. :)
make sure to have the physics raycaster component on your camera
This was the missing piece for me, thanks. I had mistakenly come to the conclusion that Event Trigger could only be used on UI elements, but with the Physics Raycaster component on my Camera, the Event Trigger works properly on 3D non-UI GameObjects, so long as they have a collider.
Answer by BudyDubby · Jul 10, 2015 at 08:36 AM
okay, i give up using Event.System on 3D object instead i'm using raycast for Select and Deselect objects, here my script, i hope its will help community
using UnityEngine;
using System.Collections;
namespace SelectionManager
{
public class SelectingObject : MonoBehaviour
{
public LoadInfo loadInfoClass;
// color of highlighted object
public Color highlightedColor = Color.cyan;
// color of selected object
public Color selectedColor = Color.blue;
// variable to store highlighted gameobject
GameObject highlightedGO = null;
// variable to store selected gameobject
GameObject selectedGO = null;
// variable to store initial color of highlighted gameobject
Color initColor;
// variable to store initial color of selected gameobject
Color initColorSelected;
Vector3 lastMousePosition;
void HighlightingGO ()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
/// HIGHLIGHT OBJECT WHEN MOUSE HOVER
/// -------------------------------------------------------------------------------------
if (hit.collider.gameObject != highlightedGO && hit.collider.gameObject != selectedGO)
{
if (highlightedGO != null)
ResetHighlightedGO();
// add highlighted object to highlightedGO
highlightedGO = hit.collider.gameObject;
// get initial color of highlightedGO
initColor = highlightedGO.GetComponent<Renderer>().material.color;
// change color of highlightedGO to show highlight effect
highlightedGO.GetComponent<Renderer>().material.color = highlightedColor;
}
/// SELECTING OBJECT WITH LEFT CLICK BUTTON
/// -------------------------------------------------------------------------------------
if (Input.GetMouseButtonDown (0)) {
lastMousePosition = Input.mousePosition;
}
// if mouse is clicked
if (Input.GetMouseButtonUp(0) && Input.mousePosition == lastMousePosition)
{
highlightedGO = null;
if (selectedGO != null)
ResetSelectedGO();
// add selected object to selectedGO
selectedGO = hit.collider.gameObject;
// get initial color of selectedGO
initColorSelected = initColor;
// change color of selectedGO to show highlight effect
selectedGO.GetComponent<Renderer>().material.color = selectedColor;
// show selected object info on side panel
loadInfoClass.ShowDescription(selectedGO.name);
print ("selectedGO is: " + selectedGO);
}
}
else
{
if (Input.GetMouseButtonDown (0))
{
lastMousePosition = Input.mousePosition;
}
// if mouse is clicked
if (Input.GetMouseButtonUp(0) && Input.mousePosition == lastMousePosition)
{
ResetSelectedGO ();
}
// if user exiting highlightedGO, reset highlightedGO color to initial
ResetHighlightedGO ();
}
}
void ResetHighlightedGO ()
{
if (highlightedGO == null)
return;
highlightedGO.GetComponent<Renderer>().material.color = initColor;
highlightedGO = null;
}
void ResetSelectedGO ()
{
if (selectedGO == null)
return;
selectedGO.GetComponent<Renderer>().material.color = initColorSelected;
selectedGO = null;
}
void Update ()
{
HighlightingGO();
}
}
}
Your answer

Follow this Question
Related Questions
EventSystem child element selection/deselection and click 0 Answers
On Void Called 2 Answers
Problem about event triggers in iPhone 0 Answers
How to pass a click through a button? 1 Answer
InputField Weird behaviour 0 Answers