Adding a gap between objects with Instantiate.

Hello, I am trying to make a grid of cubes with gaps in between each spawned object.

Here is the c# used to spawn the objects:

using UnityEngine;
using System.Collections;
 
public class WorldSpawn : MonoBehaviour {
 
    public GameObject block1; 
 
    public int worldWidth  = 10;
    public int worldHeight  = 10;
    public int gap = 1;
 
    public float spawnSpeed = 0;
 
    void  Start () {
        StartCoroutine(CreateWorld());
    }
 
    IEnumerator CreateWorld () {
        for(int x = 0; x < worldWidth; x++) {
           
 
            for(int z = 0; z < worldHeight; z++) {                
                yield return new WaitForSeconds(spawnSpeed);
 
                GameObject block = Instantiate(block1, Vector3.zero , block1.transform.rotation) as GameObject;
                block.transform.parent = transform;
                block.transform.localPosition = new Vector3(x , 0, z );
            }
        }
    }
}

As provided by an answer here by David.

I have been looking through some Q&As, I think I need to modify Vector3 somehow.

Any ideas on the best method to do this?

Thanks

Try this code:

using UnityEngine;
using System.Collections;

public class WorldSpawn : MonoBehaviour {
	
	public GameObject block1; 
	
	public int worldWidth  = 10;
	public int worldHeight  = 10;
	public int gap = 1;
	
	public float spawnSpeed = 0;
	
	void  Start () {
		StartCoroutine(CreateWorld());
	}
	
	IEnumerator CreateWorld () {
		for(int x = 0; x < worldWidth; x++) {
			
			
			for(int z = 0; z < worldHeight; z++) {                
				yield return new WaitForSeconds(spawnSpeed);
				
				GameObject block = Instantiate(block1, Vector3.zero , block1.transform.rotation) as GameObject;
				block.transform.parent = transform;
				block.transform.localPosition = new Vector3(x + x * gap, 0, z + z * gap );
			}
		}
	}
}

The only line that’s changed is the line where you edit your localPosition. You need to multiply the indices of your block by the gap to correctly offset it.