Check if RaycastHit variable is different.

Hello, I’m trying to make a script that highlights whatever object I’m looking at using raycasts.It works, except for one problem. When I look from one object to another, both objects stay highlighted instead of one of the objects losing the highlight when I look away. I know exactly why it’s happening in the code, but I don’t know how to fix it. Code:
using UnityEngine;
using System.Collections;

public class HighlightScript : MonoBehaviour {

	public RaycastHit hit;
	public Material mat, mat2;
	private bool revert = true;
	bool hasChanged = false;

	GameObject go;

	// Use this for initialization
	
	// Update is called once per frame
	void Update () {
		Vector3 fwd = transform.TransformDirection (Vector3.forward);

		if(Physics.Raycast(transform.position, fwd, out hit, 10)){
			go = hit.transform.gameObject;
			go.renderer.material = mat;
			revert = false;
			hasChanged = true;

		}
		else{
			revert = true;
		}
		if(revert)
		{
			if (hasChanged){
				go.renderer.material = mat2;
			}
		}

	}
}

It might not be the most efficient code, but I just threw it together in a couple of minutes. Basically, I want to check if the object being hit by the raycast has changed.
This is what the problem looks like, if it helps:

Try this I hope this could help:

public class HighlightScript : MonoBehaviour {
 
    public Material mat, mat2;
    private Transform lastHittedObject;    
    void Update()
	{
		Vector3 fwd = transform.TransformDirection (Vector3.forward);
		
		if(Physics.Raycast(transform.position, fwd, out hit, 10))
		{
			if(lastHittedObject != null && lastHittedObject != hit.collider.transform)
			{
                //if the lastHittedObject is not null and different from the current hitted object then
                //reset the material of the lastHittedObject
				lastHittedObject.renderer.material = mat2;
			}
			lastHittedObject = hit.collider.transform;
			lastHittedObject.renderer.material = mat;//change the hitted object's material
		}
		else if(lastHittedObject)
		{
			lastHittedObject.renderer.material = mat2;//reset the material of the last hitted object if the raycast fail
			lastHittedObject = null;
		}
	}

Hope this help :slight_smile:

This is untested, but I might restructure your code like this:

using UnityEngine;
using System.Collections;

public class HighlightScript : MonoBehaviour {
	
	public Material mat;
	public Material mat2;
	
	private Transform tr = null;
	
	void Update () {
		RaycastHit hit;
		
		if(Physics.Raycast(transform.position, transform.forward, out hit, 10.0f)){
			Transform trT = hit.transform;
			if (tr != null && tr != trT) {
				tr.renderer.material = mat2;
			}
			tr = trT;
			tr.renderer.material = mat;
			
		}
		else{
			if (tr != null) {
				tr.renderer.material = mat2;
				tr = null;
			}
		}
	}
}

//TRY THIS (I think It will work):

public class HighlightScript : MonoBehaviour {
	public RaycastHit hit;
	public Material mat, mat2;
	bool changed = false;
	GameObject go;
	// Use this for initialization
	// Update is called once per frame
	void Update () {
		if(changed == true) {
			go.renderer.material = mat2;
			changed = false;
		}
		Vector3 fwd = transform.TransformDirection (Vector3.forward);
		if(Physics.Raycast(transform.position, fwd, out hit, 10)){
			go = hit.transform.gameObject;
			go.renderer.material = mat;
			changed = true;
		}
	}
}