Best way to "highlight" an object on mouse over

Lets say I have like 25 different objects in the scene which are always visible for the camera with quite complex colliders. I then want to highlight whatever object im hovering hover(with the mouse) by changing the texture or whatever. Which is the best way to do this?..I guess I could send out raycasts every frame and change the texture whenever it hits something, but that seems quite expensive?, is there any other way?

This will be attached to all the things that can be higlighted with hovering.

private Color startcolor;
void OnMouseEnter()
{
    startcolor = renderer.material.color;
    renderer.material.color = Color.yellow;
}
void OnMouseExit()
{
    renderer.material.color = startcolor;
}

Okay so your problem with OnMouseEnter and Exit is that you are setting yourself up to be confused if you are porting to a VR platform, or a platform that has a pointer controller. If you want to keep your project open ended and not only have it releasable for PC, you can do something like below:

Create a main game object that doesn’t get destroyed so that you have a scene manager object between scenes. Make a main game script, we will prtend it’s called “MainGameScript.cs” and it’s class name is ‘MainGameScript’ and put this inside of it, above it’s class:

[System.Serializable]
public class MainInput_Ray { 
public Ray ray = new Ray ();
public RaycastHit hit;
public bool hittingSomething { get; set; }
}

Then goes in your main game script’s start:

public MG_Ray mainInput_Ray;

Start() { 
    mainInput_Ray = new MainInput_Ray();
}

…and then this goes in your main game script’s update AFTER the scene is fully loaded and playing:

Update() { 
    mainInput_Ray.ray = MG_Camera.GetComponent<Camera> ().ScreenPointToRay (Input.mousePosition);
mainInput_Ray.hittingSomething = Physics.Raycast (mainInput_Ray.ray, out mainInput_Ray.hit, 1000);
}

Now in your object’s script you can go like:

private MainGameScript mainGameScript;

Start() { 
    mainGameScript = GameObject.Find("MainGameScript"); //gameobject find is okay if it's in your start method
   //make sure you set your main game script's script execution order to FIRST
}

Update() { 
    if (mainGameScript.mainInput_Ray.hittingSomething && (mainGameScript.mainInput_Ray.hit.transform == this.transform.parent.transform)) {
 
   }
}

Then throughout your entire project whenever you need to check if something is hit you do mainInput_Ray.hittingSomething == true???, check the results, throw an && in the if statement, just make sure the scene if fully loaded. Personally I have a state machine in my main game object that handles the loading screen, saving and loading of data and has a ‘SceneActive’ state I check against first.

Now if you have a VR controller you just have to replace this part:

        mainInput_Ray.ray = MG_Camera.GetComponent<Camera> ().ScreenPointToRay (Input.mousePosition);

This code is optimized just like OnMouseEnter, because you aren’t creating a new ray all together each and every frame, there is only one global ray you check against.

I’m updating the answer since a lot of links trace to this post despite it being depreciated.


private Color startcolor;
    void OnMouseEnter()
    {
        startcolor = GetComponent<Renderer>().material.color;
        GetComponent<Renderer>().material.color
= Color.red;
    }
    void OnMouseExit()
    {
        GetComponent<Renderer>().material.color
= startcolor;
    }

function OnMouseEnter?