EventTrigger on 3D object not working?

I want to make a Select and Deselect method on 3D object. I’m using Event Trigger 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"); }
    }
}

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. :slight_smile:

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();
		}
	}
}

Hey;
You can try these following steps:

  1. Add EventTrigger to the 3d gameObject.
  2. Choose PointerDown as Event Type.
  3. Add this to your script.
public class MainController : MonoBehaviour, ISelectHandler
{
    public void OnSelect(BaseEventData eventData)
    {
        PointerEventData pointerEventData = eventData as PointerEventData;
        Debug.Log("data = "  + pointerEventData.pointerCurrentRaycast.gameObject .name);
    }
}