raycast not working

the following code does not execute at all, its attached to the player control module (custom fps controller)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SelectionManager : MonoBehaviour
{
[SerializeField] private Material HighlightMaterial;
void Start()
{

}
private void Update()
{
var ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2f, Screen.height / 2f));
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 6f))
{
var selection = hit.transform;
var selectionRenderer = selection.GetComponent();
if (selectionRenderer != null)
{
selectionRenderer.material = HighlightMaterial;
}
}
}
}

  • Use Code tags when posting code on the forums.
  • Try to be specific. This code isn’t be executed? Or it isn’t producing the output you expect?
  • Add Debug.Log statements to see what code is getting executed, and what the values are.
  • Use Debug.DrawLine to help you to picture what the raycast is doing. This will only be shown in the Scene view, but it helps confirm you’ve got your direction vector correct.
  • Make sure this script is actually on a game object in your scene.
  • Try increasing max distance of the raycast.