Search Unity

Getting OnMouseEnter and Exit refs from parent GameObject

Discussion in 'Scripting' started by Studio_Akiba, May 24, 2015.

  1. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I have been trying to do this for the past 10 minutes and finding almost nothing useful on the internet.

    I have a script on a trigger object to detect the player, but I also need to detect the OnMouseEnter and OnMouseExit events on the parent GameObject which has the actual model on it, as I said, I have found nothing on this at all so I am not even sure if it is easily possible.
     
  2. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
  3. Jodon

    Jodon

    Joined:
    Sep 12, 2010
    Posts:
    434
    I've had some issues with OnMouseEnter not firing unless you have a Collider & Rigidbody and it's the FIRST collider that hits your raycast from the camera. In this situation, you may have issues if you're expecting the parent to receive the event, but also the children to receive it as well. If that IS the case you're experiencing (check with Debug.Log to make sure), then you can have the parent object re-broadcast the message via the MonoBehaviour.BroadcastMessage function.
     
    krougeau likes this.
  4. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    My biggest problem using this at the moment is that I had a working script that triggered a UI object to be on when I am both inside a sphere collider AND looking at an object, this worked fine.
    Then I duplicated the object, both work fine and the OnMouseEnter and Exit functions are being picked up for both, but only 1 object seems to trigger the UI object, more than that, it chooses a different one every time Unity starts up.
    First the left one triggered it, then the right and now the left again.