title: Unity2D - Randomly instantiated objects need to not spawn close to each other
body: <p>I'm a beginner to Unity. I'm creating an educational snake game that randomly spawns three potential answers. It's working, however I need to not have them spawn close to each other. I also need not to have any of them spawn near the player/snake. Someone already tried to help me and I asked him for help, but it seems like he's unable to fix the problem and the objects still spawn near each other. </p>
  
  <pre><code>void GeneratePotentialAnswers()
  {
      allAnswers.Clear();
      List&lt;Vector3&gt; AnswerPositions = new List&lt;Vector3&gt;();
      AnswerPositions.Add(player.transform.position);
      for (int i = 0; i &lt; potentialAnswers.Count; i++)
      {
          GameObject ans = Instantiate(enemyAnswerPrefab);
  
          Vector3 pos;
          int index = 0;
          bool tooClose = false;
          do
          {
              float rndWidth = Random.Range(15f, Screen.width - 25f);
              float rndHeight = Random.Range(15f, Screen.height - 105f);
              pos = Camera.main.ScreenToWorldPoint(new Vector3(rndWidth, rndHeight, 0f));
              pos.z = -1f;
              foreach (Vector3 p in AnswerPositions)
              {
                  float distance = (pos - p).sqrMagnitude;
                  if (distance &lt; 3f)
                  {
                      tooClose = true;
                  }
              }
              index++;
          }
  
          while (tooClose == true &amp;&amp; index &lt; 500);
          Debug.Log(index);
  
          AnswerPositions.Add(pos);
  
          ans.transform.localPosition = pos;
          ans.GetComponent&lt;Answer&gt;().potentialAnswer = potentialAnswers[i];
          ans.GetComponent&lt;Answer&gt;().addToScore = scorePerCorrectAnswer;
          ans.GetComponent&lt;Answer&gt;().SetAnswer();
          allAnswers.Add(ans.GetComponent&lt;Answer&gt;());
      }
  }
  </code></pre>