# How to Spawn GameObjects at Random Positions in Unity? [closed]

Hello GameDev Community,

I have a problem with this pice of code. If I start the "Game", it crashes right away. Probably is its the list of GameObjects or the foreach loop. I have no idea how I can solve the problem. Please don't be too strict with me [...]

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public class TreeGeneratorScript : MonoBehaviour
{
// ####################################################################################################

// GameObject: Tree
public GameObject prefabTree;

public Vector3 minGroundPos;  // Vector3(  0, 1,  0)
public Vector3 maxGroundPos;  // Vector3(100, 1, 50)

// List of GameObjects with our Trees
public List<GameObject> treeList = new List<GameObject>();

// Number of attempts ( 100 )
public int numRunTry;

// Variables
GameObject newTree;
float distance;

// ####################################################################################################

void Start()
{
// Instantiate a tree in the middle of the Scene
newTree = Instantiate(prefabTree, new Vector3(50, 1, 25), Quaternion.identity);

// Add GameObject to our List

// Call Method: SpawningTrees
SpawningTrees();
}

// ####################################################################################################

void SpawningTrees()
{
// Try 100 times to spawn a tree
for (int i = 0; 1 < numRunTry; i++)
{
// Get a Random Position
Vector3 randomSpawnPos = new Vector3(
Random.Range(minGroundPos.x, maxGroundPos.x),
1,
Random.Range(minGroundPos.z, maxGroundPos.z)
);

foreach(GameObject tree in treeList)
{
// Calculate the distance between our Random Position and each tree from our list
distance = Mathf.Sqrt( Mathf.Pow( (tree.transform.position.x - randomSpawnPos.x), 2) + Mathf.Pow( (tree.transform.position.z - randomSpawnPos.z), 2));

// Check if the distance is smaller 12 between a tree and the Random Position
if(distance < 12)
{
// Random Position is too close to our tree -> new Random Position
break;
}
else if(distance >= 12 && tree == treeList.Last())
{
// Every tree from our list has a min distance of 12 to our Random Position

// Instantiate the final tree
newTree = Instantiate(prefabTree, randomSpawnPos, Quaternion.identity);

// Add tree to our list of trees

break;
}
}
}
}


After I run the code, it should look like this: • Apps rarely "just crash". They are tooled to tell you why they crashed, specially those over which you have control. What other info can you give us about "the crash"? Oct 12, 2021 at 18:11
• and that's hint :) no error message because he was entering an endless loop. Oct 12, 2021 at 18:15
• Your problem isn't "how do I spawn objects randomly", it's "my app hangs when I run this for loop". That's a general programming problem, not one specific to game development, so it belongs on StackOverflow. Oct 12, 2021 at 18:28
• Nonetheless, illustrative of looking at a problem through the wrong lens. Experienced Unity programmers know that when the whole shebang hangs it's almost always an endless loop condition. But beginners, maybe not so much able to tell the difference between a crash and a hang... OP was probably convinced that problem had something to do with spawning per se and was looking for answer there... Oct 12, 2021 at 19:28

for (int i = 0; 1 < numRunTry; i++)


1 is always less than numRunTry so loop never ends. Probably a typo and you meant

for (int i = 0; i < numRunTry; i++)


Correct that and it works fine: • Thanks, a small mistake with a big impact. Oct 12, 2021 at 18:29

I'm by no means equipped properly to answer this question, so forgive me if I am wrong. However, I notice in spawning trees method you are attempting to spawn 100 trees, it would appear? Have you tried just spawning a lesser amount to see if that is to many for it to handle or not?