Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

How replace OnMouseDown in the new Input System?

Discussion in 'Scripting' started by KokodokoGames, Sep 13, 2020.

  1. KokodokoGames

    KokodokoGames

    Joined:
    May 20, 2014
    Posts:
    40
    I want to detect if a player clicked on a specific gameobject. I used to do this with OnMouseDown, but that no longer works in the new input system. In the new system, you can have click actions, but then you detect clicks in the entire screen instead of on one single gameobject.

    Code (CSharp):
    1. public InputAction clickAction;
    2.  
    3. void Awake() {
    4.       clickAction.performed += ctx => OnClickedTest();
    5. }
    6.  
    7. void OnClickedTest(){
    8.       Debug.Log("You clicked anywhere on the screen!");
    9. }
    10.  
    11. // this doesn't work anymore in the new system
    12. void OnMouseDown(){
    13.       Debug.Log("You clicked on this specific object!");
    14. }
    How can I detect clicks on a single gameobject with the new input system?
     
    alexu likes this.
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    You can use ScreenPointToRay off your camera to cast into the scene from the click:

    https://docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html

    From that raycast (or raycastall) you can see what you might have hit.

    In fact you could pretty easily reconstruct a generic form of the old click detect by using .SendMessage() to the object that you hit via the above method.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    I don't think the new Input system changes anything related to OnMouseDown. You may be confusing the Event System with the Input System. What you may be missing though, is that there's a component that ties the Input System together with the Event System, and that is the Input Module. In the new Input System you need an "Input System UI Input Module" instead of the old "Standalone Input Module".

    OnMouseDown is also an older less flexible form of https://docs.unity3d.com/2019.1/Doc...ystems.IPointerDownHandler.OnPointerDown.html
     
    Ryiah likes this.
  4. ReggieBeRetro

    ReggieBeRetro

    Joined:
    Aug 30, 2015
    Posts:
    73
    I ran into a similar problem the other day but I was instantiating objects from the resource folder and OnClick would never save if I assigned it manually in the inspector. etc bottom line I had to use the EventSystems as PraetorBlue describe above.

    Here is the snipet I used. (Change USER-Element) to the name of the object you want to click.


    Code (CSharp):
    1. using UnityEngine.EventSystems;
    2. public static GameObject modbtn;
    3.  
    4. public static GameObject GetEventClickedButton ()
    5.     {
    6.         EventSystem currentEvent = EventSystem.current;
    7.         //Debug.Log (currentEvent.currentSelectedGameObject);
    8.         if (currentEvent.currentSelectedGameObject.ToString ().Contains ("USER-Element")) {
    9.             modbtn = currentEvent.currentSelectedGameObject.gameObject;
    10.         }
    11.         return currentEvent.currentSelectedGameObject;
    12.     }
     
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,137
    This. I'm a little surprised that the documentation doesn't make any mention of it because it's clearly confusing a number of people. In fact that basically sums up my entire experience with the new system to date. Many little details that you're never told but have to know to properly use it.
     
    Last edited: Sep 14, 2020
  6. Saulotti

    Saulotti

    Joined:
    Oct 20, 2009
    Posts:
    14
    Source: https://docs.unity3d.com/2019.1/Documentation/ScriptReference/EventSystems.IPointerClickHandler.html

    Interface to implement if you wish to receive OnPointerClick callbacks.

    Use the IPointerClickHandler Interface to handle click input using OnPointerClick callbacks. Ensure an Event System exists in the Scene to allow click detection. For click detection on non-UI GameObjects, ensure a PhysicsRaycaster is attached to the Camera.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class Example : MonoBehaviour, IPointerClickHandler
    5. {
    6.    //Detect if a click occurs
    7.    public void OnPointerClick(PointerEventData pointerEventData)
    8.    {
    9.        //Output to console the clicked GameObject's name and the following message. You can replace this with your own actions for when clicking the GameObject.
    10.        Debug.Log(name + " Game Object Clicked!");
    11.    }
    12. }
     
    vidocco and ATate like this.