Search code examples
c#eventsinputunity-game-engineunityscript

detecting mouseClick/touch on gameObject


the system never detect my mouse/touch Click on a gameObject (the right or left arrow , check the below link)

public void OnMouseDown()
{
    //don`t enter here!!
    if (this.name == "gameObjectName")
       doAction();
}

enter image description here


Solution

  • Assuming your script is attached to the Left Arrow-50 game object, first of all make sure both of them have collider components.

    After that, rewrite your code removing the if line. Only the instance of your script that is attached to the clicked object will be called, so you don't need this line to check if this is the right object.

    public void OnMouseDown()
    {
        doAction();
    }
    

    Do the same with the Right Arrow-50 and its script.