Conditional OnMouseEnter/Exit

I have a simple scene where a camera rotates around a group of objects.

I would like objects that I roll over with the mouse to highlight yellow and un-highlight when I move off the object. However, I only want this to happen if the space bar is being held down. In other words, rollovers are only activated if the space bar is actively being held down.

Any help is greatly appreciated!

I have the rollovers partially working with this:


var initialColor : Color;

function Start () {
     initialColor = renderer.material.color;
}

function OnMOuseEnter() {
     renerer.material.color = color.yellow;
}

function OnMOuseExit() {
     renerer.material.color = initialColor;
}

if(Input.GetKey(KeyCode.Space))
{
//What you want to do when space is being held down
}

Thanks for your response Em3rgency. However, I’m fairly new to coding in Unity, and I don’t understand how to implement your answer.

For example, In the code snippet I posted above, I don’t believe I can put my function declaration in an ‘if’ statement. And, if I don’t put it in my ‘if’ statement and try to simply call the function from the ‘Update’ function, there are errors since the OnMouseEnter/Exit functions are called every frame anyway.

In other words, I’m not clear on where to declare the functions and what function to call them from.

Sorry, I hope my explanation is not too convoluted.

Thanks again for your help!

Try the following:

        var initialColor : Color;
         
        function Start () {
        initialColor = renderer.material.color;
        }
         
        function OnMOuseEnter() {
        if(Input.GetKey(KeyCode.Space))
           {
            renderer.material.color = color.yellow;
           }
         }
        function OnMOuseExit() {
        if(Input.GetKey(KeyCode.Space))
           { 
            renderer.material.color = initialColor;
           }
        }