title: How can i Instantiate objects in a group?
body: <pre><code>public void generateObjectOnTerrain()
      {
          objectPool = new ObjectPool(objectToInstantiate, objectsToInstantiate);
  
          float randX = 0f;
          float randZ = 0f;
  
          for (int i = 0; i &lt; objectsToInstantiate; i++)
          {
              if (random == true)
              {
                  //Generate random x,z,y position on the terrain
                  randX = UnityEngine.Random.Range(xTerrainPos, xTerrainPos + terrainWidth);
                  randZ = UnityEngine.Random.Range(zTerrainPos, zTerrainPos + terrainLength);
              }
              else
              {
  
              }
  
              float yVal = Terrain.activeTerrain.SampleHeight(new Vector3(randX, 0, randZ));
  
              var randScaleX = Random.Range(RandScaleMin.x, RandScaleMax.x);
              var randScaleY = Random.Range(RandScaleMin.y, RandScaleMax.y);
              var randScaleZ = Random.Range(RandScaleMin.z, RandScaleMax.z);
              var randVector3 = new Vector3(randScaleX, randScaleY, randScaleZ);
  
              //Apply Offset if needed
              yVal = yVal + yOffset;
  
              clonedObject = objectPool.GetInstance();
  
              if (randomScale == true)
                  clonedObject.transform.localScale = randVector3;//new Vector3(randScaleX, randScaleY, randScaleZ);
  
              if (parent)
                  clonedObject.transform.parent = this.transform;
  
              clonedObject.tag = "ClonedObject";
  
              clonedObject.transform.position = new Vector3(randX, yVal, randZ);
          }
  
          createdObjects = GameObject.FindGameObjectsWithTag(objname);
  
          UpdateList(false, "ClonedObject");
      }
  </code></pre>
  
  <p>This will Instantiate objects in random places over the terrain area.</p>
  
  <p>I want in the else when it's not random to place the objects in a group with two main parameters/variables, one to set the space between the objects the second to set how to make them in lines for example if in objectsToInstantiate there are 20 objects and i set a variable to 4 and another variable to 10 then the objects will be in lines of 4 that is 5 lines and the space between the objects will be 10 from every side.</p>
  
  <p>If i have 55 objects and i set to make it in lines of 4 then there will be 14 lines of 4 the last line will be with 3 objects.</p>
  
  <p>For example a paint of 4 objects in a row and 5 lines in space of 10:</p>
  
  <p><a href="https://i.stack.imgur.com/cNoWZ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cNoWZ.jpg" alt="Example"></a></p>
  
  <p>And in case there are for example 23 objects then it will look like that:</p>
  
  <p><a href="https://i.stack.imgur.com/e1kdh.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/e1kdh.jpg" alt="Example1"></a></p>