Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by BudyDubby · Jul 08, 2015 at 10:13 AM · eventsystemevent triggeringselecteddeselect

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");
         }
     }
 }
 
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nerevar · Jul 08, 2015 at 12:47 PM 0
Share

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.

avatar image BudyDubby · Jul 10, 2015 at 02:06 AM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MaryamKamel · Oct 15, 2018 at 04:19 PM 1
Share

make sure to have the physics raycaster component on your camera

avatar image beard-or-die MaryamKamel · Mar 26, 2019 at 05:49 PM 0
Share

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.

avatar image
0
Wiki

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges