Search code examples
c#unity-game-engineonmouseover

OnMouseOver() doesn't work on children


a semi-newbie here!

I want to make a hitbox for another object I want to drag around with my mouse in Unity3D. My hitbox is a sphere with a collider and my main object is a fish sprite. I've made a simple script to check if the mouse is on the hitbox-object:

void OnMouseOver ()
{
    hover = true;
}


void OnMouseExit ()
{
    hover = false;
}

The script is attached to the hitbox. I can see whether the public 'hover' boolean is true or not from the inspector. While my hitbox isn't 'connected' with any other objects, this works as intended. If I make the fish sprite a child of my hitbox, it still works. However, once I make the hitbox a child of the fish sprite, it no longer works.

Why is that and is there a simple way around it?

I want the hitbox to follow the fish sprite around. The fish sprite will later be a rigidbody that I want to interact with the rest of my game.


Solution

  • OnMouseOver event will NOT work on child objects if the root parent has a rigidbody.

    I suggest using raycasting to detect OnMouseOver as an alternative.

    Reference: http://answers.unity3d.com/questions/241844/child-objects-trigger-collider-not-working-when-pa.html