Keypress interferes with mouse hover

Hello,

I have a script that requires the player to hover the mouse over an object and hold a key down (z). I set it up so the GUI displays some information OnMouseEnter, and clears that information OnMouseExit. However, it seems that holding down z rapidly activates and deactivates the mouse cursor, so that it switches between Enter and Exit, causing the GUI to flicker. Does anyone know how to make held down keys leave the mouse alone?

Thanks for taking a look at this.

EDIT: Here's an update with some script: Here's what's on the object to display and clear the GUI data:

function OnMouseExit () {

guidata.tipname = null;

guidata.tiphits = 0;

guidata.tiphead = null;

guidata.tiptorso = null;

guidata.tiplegs = null;

}

<p>function OnMouseOver () {</p>

var unitdata = gameObject.GetComponent(state);

guidata.tipname = unitdata.unitid;

guidata.tiphits = unitdata.hits;

guidata.tiphead = unitdata.head;

guidata.tiptorso = unitdata.torso;

guidata.tiplegs = unitdata.legs;

}

Hmm, the thing killed my formatting, but I think you can tell what's going on.

And here's the keypress:

function Update () {

if (Input.GetButton("z")){

gameObject.renderer.enabled = true;

}

if (!Input.GetButton("z")) {

gameObject.renderer.enabled = false;

}

}

I haven't tested with your code, so I can't see the described problem, but there are a few things I noticed:

  • You should probably do the GetComponent once, in Start(), rather than every time you release the mouse.

  • You should probably rewrite your Update keypress script to use GetButtonUp/GetButtonDown.

For example:

function Update ()
{
   if ( Input.GetButtonDown("z"))
   {
      gameObject.renderer.enabled = true;
   }
   else if ( Input.GetButtonUp("z"))
   {
      gameObject.renderer.enabled = false;
   }
}

Edited to remove OnMouseUp/OnMouseExit references (wrong code was pasted into question).