Change Color of Object when NOT looking at it

Hey so what I’m trying to do is, look at an object, it turns yellow. If I’m not looking at the object, it turns back to red. I get it to yellow fine, but going back to red has me stumped. The ‘else’ statement below does nothing, doesn’t even register a Debug.Log. Not sure what to do. Any ideas would be great, thanks!

.

public float rayLength;

public GameObject player;

void Update()
{
	RaycastHit hit;

	Ray ray = new Ray (transform.position, transform.forward);

	Debug.DrawRay (transform.position, transform.forward * rayLength);

	if (Physics.Raycast (ray, out hit, rayLength)) 
	{
		PlatformDummyScript platforms = hit.transform.GetComponent<PlatformDummyScript> ();

		if (platforms != null) 
		{
			if (platforms) 
			{
				platforms.GetComponent<MeshRenderer> ().material.color = Color.yellow;

				if (Input.GetKeyDown (KeyCode.G)) 
				{
					Vector3 offset = new Vector3 (0, 1, 0);

					player.transform.position = platforms.transform.position + offset;
				}
			} 

			else 
			{
				platforms.GetComponent<MeshRenderer> ().material.color = Color.red;
			}
		} 
	} 

}

private PlatformDummyScript lastHitPlatform ;

void Update()
{
    RaycastHit hit;
    Ray ray = new Ray (transform.position, transform.forward);
    Debug.DrawRay (transform.position, transform.forward * rayLength);
    if (Physics.Raycast (ray, out hit, rayLength)) 
    {
        PlatformDummyScript platform = hit.transform.GetComponent<PlatformDummyScript> ();
        if (platform != null) 
        {            
            if( lastHitPlatform != null && platform != lastHitPlatform )
                DeselectPlatform( lastHitPlatform ) ;
            
            SelectPlatform( platform ) ;
            
            if (Input.GetKeyDown (KeyCode.G)) 
            {
                Vector3 offset = new Vector3 (0, 1, 0);
                player.transform.position = platform.transform.position + offset;
            }
        } 
    }
    else if( lastHitPlatform != null )
        DeselectPlatform( lastHitPlatform ) ;
}

private void SelectPlatform( PlatformDummyScript platform )
{
    platform.GetComponent<MeshRenderer> ().material.color = Color.yellow;
    lastHitPlatform = platform ;
}

private void DeselectPlatform( PlatformDummyScript platform )
{
    platform.GetComponent<MeshRenderer> ().material.color = Color.red;
    lastHitPlatform = null ;
}