OverlapSphere not updating, despite being in update function

Hi. I basically have a script attached to my player object, and in the update function I am using Physics.OverlapSphere to detect nearby objects. This does work, but only in the area the player starts at. If the player moves the list OverlapSphere is generating every frame isn’t updating.

Here’s my code:

void Update () 
{
    Collider[] hitColliders = Physics.OverlapSphere(this.transform.position, 1);
        int i = 0;
	while (i < hitColliders.Length) {
		hitColliders*.gameObject.renderer.material.color = Color.red;*
  •  i++;*
    
  • }*
    }
    All I want it to do is update, so every frame it is updating hitColliders with the list of gameObjects that are actually nearby.
    Need help with this pretty bad, don’t know where else to ask!

Seems to work. Are you getting any errors?

Try this one:

void Update () 
    {
	    Collider[] hitColliders = Physics.OverlapSphere(this.transform.position, 40);
        Debug.Log("Colliders length : " + hitColliders.Length);
        int i = 0;
        while (i < hitColliders.Length)
        {
            Renderer red = hitColliders*.gameObject.GetComponent<Renderer>();*

if(red != null)
{
red.material.color = Color.red;
}

i++;
}

  • }*