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. Dismiss Notice

OnMouseDown does not activate

Discussion in 'Scripting' started by Inexperienced, Jan 30, 2015.

  1. Inexperienced

    Inexperienced

    Joined:
    Feb 5, 2014
    Posts:
    8
    Hello. I have a prefab instantiated from code with a script attached which has an OnMouseDown function. This function never ends up being called. I have tried attaching the script to the base prefab, as well as to the mesh inside. The mesh has a box collider.

    After some Googling I found an example script to raycast from the mouse, and attached the script to my camera.
    Code (csharp):
    1.  
    2. void Update () {
    3.         if (Input.GetMouseButtonDown (0)) {
    4.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    5.             RaycastHit hit;
    6.             if (Physics.Raycast(ray, out hit)) {
    7.                 Debug.Log ("Name = " + hit.collider.name);
    8.                 Debug.Log ("Tag = " + hit.collider.tag);
    9.                 Debug.Log ("Hit Point = " + hit.point);
    10.                 Debug.Log ("Object position = " + hit.collider.gameObject.transform.position);
    11.                 Debug.Log ("--------------");
    12.             }
    13.         }
    14.     }
    15.  
    Upon playing the game, the ray cast is positive for all of my instantiated prefabs (they don't have names or tags, but the object positions match those of the prefabs), yet OnMouseDown is still never called.
     
  2. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    Can you post your code that contains OnMouseDown?
     
  3. Inexperienced

    Inexperienced

    Joined:
    Feb 5, 2014
    Posts:
    8
    The short function:
    Code (csharp):
    1.  
    2. void OnMouseDown() {
    3.         Debug.Log ("CLICKED");
    4.         if (!flipping) {
    5.             flipping = true;
    6.  
    7.             if (!flipped) {
    8.                 flipDirection = 0;
    9.             }
    10.         }
    11.     }
    12.  
    CLICKED never appears in the log, neither does the rest of the customized code activate. I can post the entire script if you'd like, but I doubt it will reveal anything more.
     
  4. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    Yes, if you can post more code, it will help..also, if possible, please post
    the code that you're using to Instantiate the prefab.
    Just a wild guess is that maybe its not derived from a MonoBehaviour
    or the prefab does not exist/initialized fully at the time you are expecting
    OnMouseDown() to run.
     
  5. Inexperienced

    Inexperienced

    Joined:
    Feb 5, 2014
    Posts:
    8
    The code that instantiates: http://pastebin.com/DczrvQ80
    The code on the object that is instantiated: http://pastebin.com/BcvAZMgR

    All of the prefabs are public variables and are set inside the Unity editor. "prefab" in "AreaController" is a prefab with a "ParkTileScript" attached.

    edit:
    Yes... the objects will be fully created and initialized by the time I click my mouse button. You can tell because you can see the cubes in the game. Cubes not there? Not instantiated yet. It also doesn't matter if I wait until the moment they're visible or 20 seconds after the fact, and this does not explain why doing a manual ray trace succeeds whereas using OnMouseDown fails.

    Thanks for your interest though.
     
    Last edited: Jan 30, 2015
  6. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Does the object belong to the ignore raycast layer by any chance?
     
  7. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    Sure, np.

    Two things come to mind :

    1. Ignore Raycast Layer - Unity manual says that "This function is not called on objects that belong to Ignore Raycast layer."

    2. OnMouseDown seems to have a depth limit to which it will detect objects, so maybe the solution is
    to just use Raycast as you've already done.
     
  8. Inexperienced

    Inexperienced

    Joined:
    Feb 5, 2014
    Posts:
    8
    Layer is currently set to default on both the mesh and the prefab.


    ---EDIT---
    WOW. I just restarted the IDE and with 0 code changes, it literally started working. Funny thing... I actually was recording my screen at the time...

    Link to movie (warning: 45 MB, music, mp4): https://dl.dropboxusercontent.com/u/1215621/rec (05).mp4

    Using... Unity 4.6.1p5. Maybe 4.6.2 has this fixed.
     
    Last edited: Jan 30, 2015
    flaminghairball likes this.