Search Unity

Event Trigger and Click Handler on 3D Object (Cube) Problem

Discussion in 'UGUI & TextMesh Pro' started by Denin, Feb 25, 2019.

  1. Denin

    Denin

    Joined:
    Mar 5, 2015
    Posts:
    27
    Hello,

    I want to have a function called when I click on a 3D object and I am trying to use and Event Trigger since I'm following along with the Adventure Sample Game tutorial from Unity.

    I have a simple setup that I can't get to work no matter what I do.

    Here is my setup:
    I have 5 objects in my scene.


    1) A cube collider which has:
    -A box collider that is the size of the cube
    -Event Trigger for Pointer Up which calls a function called "HandleClick" in the ClickHandler script which is attached to a game object that is also called "ClickHandler". I set up this connection in the Editor (although I have also created the event trigger through code and it doesn't work there either).


    2) A cube with default values.


    3) A Camera called Main Camera
    -Has a Physics Raycaster on it
    -Has an Event System on it

    4) A default directional light

    5) A game object with the Click Handler Script attached.


    Here is the Code for the Click Handler:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.EventSystems;
    4.  
    5. public class ClickHandler : MonoBehaviour
    6. {
    7.     public void HandleClick()
    8.     {
    9.         Debug.Log("Cube Clicked");
    10.     }
    11. }
    12.  

    I am using Unity version 2018.3.5f1 Personal.

    I have searched everywhere and tried everything I can think of but I can't figure out what I'm missing. Can anyone see where I'm going wrong? Any help would be greatly appreciated. Thanks!
     
    Last edited: Feb 26, 2019
  2. Denin

    Denin

    Joined:
    Mar 5, 2015
    Posts:
    27
    *Update*
    Here are some more things I have tried, but I'm still having the problem.

    -Uninstall and reinstall Unity
    -use Graphics Raycaster instead of Physics Raycaster
    -Put the ClickHandler script on the cube collider

    This seems like a very simple case, can't figure out what I'm missing. Any suggestions would be great.
     
  3. Denin

    Denin

    Joined:
    Mar 5, 2015
    Posts:
    27
    *Update*
    I finally managed to solve this issue. If anyone else is having the same problem, the answer was that I was missing a component for this to work. I needed to add a "Standalone Input Module". I added it to the same object as the Event System and everything worked.
     
    ukzukz and Neogene like this.
  4. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Did you try to debug, which gameobject you are hitting with the physics raycaster? Maybe your collider or your mesh is stopping the other to be reached.
     
  5. IngJuego

    IngJuego

    Joined:
    Jul 12, 2019
    Posts:
    1
    gracias....