Still strugling trying to get the following code to be conditional upon the Space Bar being held down.
var initialColor : Color;
function Start () {
initialColor = renderer.material.color;
}
function OnMOuseEnter() {
renerer.material.color = color.yellow;
}
function OnMOuseExit() {
renerer.material.color = initialColor;
}
The above works fine, however, I only want the OnMouseEnter/Exit to work if the Space Bar is being held down.
Someone replied to a previous post on the subject by suggesting I just use “if(Input.GetKey(KeyCode.Space))”. However, this doesn’t work since I can’t declare my funciton in an ‘if’ statement.
Furthermore, I can’t simply call the functions declared above in the ‘Update’ function since they are already being called every frame, which overides my ‘Update’ call.
Any suggestions? Seems so simple but it’s driving me nuts!
Thanks in advance
You don’t declare a function inside an if statement since an if statement can only be placed inside a function. So just do
function OnMouseEnter()
{
if(Input.GetKey(KeyCode.Space))
renerer.material.color = color.yellow;
}
So whenever the user “enters” the collider it’s only highlighed when you hold the space bar while you enter the collider.
If you want to highlight it when you press enter while you hover over the collider you should use OnMouseOver instead and use GetKeyDown
function OnMouseOver()
{
if(Input.GetKeyDown(KeyCode.Space))
renerer.material.color = color.yellow;
}
btw: OnMouseExit might be used unchanged, but it depends on your desired behaviour.
Thank you! I don’t know why I didn’t see that.
The script is working now, however, not exactly as desired:
Currently, you have to press the space bar when the mouse is directly over the object for it to turn yellow. It doesn’t work if you are holding the space bar down while moving the mouse and then move over the object. Is there a way to correct this?