title: Detecting proximity of multiple objects and changing their material color
body: <p>I have a problem with the correct detection of object proximities using distance calculations and hope one of you can help me.</p>
  <p><strong>What i want:</strong>
  There are several instantiated game objects with the same tag in my scene and I want to change their material color if their distance on the x and z axis is below &quot;1&quot;. For this I iterate over a list of all objects and compare their position with the position of the current game object.</p>
  <p><strong>The problem:</strong>
  Material colors are changing randomly on colliding objects and sometimes don't change back once the collision is over.</p>
  <p><strong>My code so far:</strong></p>
  <pre><code>     public class DistanceTester : MonoBehaviour
   {
       void Start()
       {
   
       }
   
       void Update()
       {
           var menschen = GameObject.FindGameObjectsWithTag(&quot;Mensch&quot;);
           float positionX = transform.position.x;
           float positionZ = transform.position.z;
   
           foreach (GameObject mensch in menschen)
           {
               float distanceX = Mathf.Abs(mensch.transform.position.x - positionX);
               float distanceZ = Mathf.Abs(mensch.transform.position.z - positionZ);
   
               if (gameObject != mensch) //Making sure the object is not the same
               {
                   if (distanceX &lt;= 1 &amp;&amp; distanceZ &lt;= 1)
                   {
                       GetComponent&lt;Renderer&gt;().material.color = Color.red;
                       mensch.GetComponent&lt;Renderer&gt;().material.color = Color.red;
                   }
                   else
                   {
                       GetComponent&lt;Renderer&gt;().material.color = Color.green;
                       mensch.GetComponent&lt;Renderer&gt;().material.color = Color.green;
                   }
               }
           }
       }
   }
  </code></pre>
  <p>I already tried to use triggers for collision detection but would like to use a more efficient way like in my example above.</p>