Inconsistent OnMouseEnter caused by Instantiate

Alright so I’ve been pouring over this issue for several hours now and double checked everything that I possibly know. Here is what is happening.

I’m working on a board game. I have the board spaces and pieces set to highlight OnMouseOver. The spaces have worked fine, and MOST of the pieces work fine. Pieces are created dynamically through instantiate at particular board spaces. Here are relevant details:

  • Pieces that are built from the same prefab are not consistent with each other; say I spawn two pawns, one works OnMouseOver and the other doesn’t
  • Sometimes swapping to a different board space fixes it, sometimes it doesn’t
  • I’ve double checked all colliders while debugging and they are still all there
  • There are no overlapping colliders getting in the way of mouse overs
  • Swapping the order of the piece spawning does affect which pieces work and which don’t
  • Creating multiple lines of Instantiate changes which pieces work and which don’t; there is no discernible pattern (if 4 pieces don’t work, adding 4 more instantiates doesn’t “push” the bad instances off of a stack)
  • All of the properties of instanced objects from the same prefab are all the same
  • Making all of the instancing happen from only one of the prefabs does not change anything (so it is just the act of instantiating)

Any ideas on what could be going on? I thought it was something wrong within my own code, but after isolating the problem the only line that changes anything is the instantiate lines and I’ve separated out the casting as well so it is just:

Object obj = Instantiate(prefab);

Duplicating that line creates different results based on how many lines there are. It is still consistent between runs (6 lines of instantiate causes the same pieces to not trigger OnMouseOver every time, but 7/8/9/etc lines of instantiate are all different).

Also yes the pieces must be instantiated at runtime as the pieces will eventually be chosen by the user to create their “team” and they can have multiples of a single piece. So having a pre-created pool of max number of multiples for each type of piece is unfeasible. I’m running on the latest version of Unity 4 Pro: 4.0.0f7.

EDIT: Another interesting detail… if I manually change the coordinates during debugging through the inspector and then change it back it starts working when I switch back to the game tab. ??? Does this make any sense?

EDIT2: None of my objects are on the IgnoreRacast layer.

EDIT3: Alright so changing the coordinates in the inspector fixes the issue, but moving it in the scene with the move tool does not fix it… very strange indeed.

Found my problem… this can be closed. The issue was that I had generated a collider for the top level of the prefab and used the mesh from the child. What I didn’t realize was that I also had generate colliders for my models on so the child also had a collider on it. Sometimes the Raycast would hit the top level object, but on the pieces that were broken it was hitting the bottom level and hence not triggering my top level OnMouseEnter code. :slight_smile: Finally glad to have this fixed after 5 hours. This code is a beautiful thing:

RaycastHit hitInfo;

if( Physics.Raycast( Camera.main.ScreenPointToRay( Input.mousePosition ), out hitInfo )) {
    Debug.Log( "mouse is over object " + hitInfo.collider.name );
}

I gave same issue, I have been spending a bunch of time on this so far. I do not get your solution though… In My Prefab, I have a parent GameGameObject with couple childeren… The Box Colldier2D is on the Outer GameObject, that covers all the children.

I double checked that the game object’s “Is Trigger” is enabled and applied to all the PreFabs as well.

Could you explain what exactly is your issue and how did you fix it?