Instantiate object at certain position

Hi, I’ve been trying to instantiate an object in a certain position (for example at the center but retaining the spaces in between each cell) I’m new in unity and I’m having a hard time manipulating the code. Thankyou for for help. :slight_smile:

using UnityEngine;
using System.Collections;

public class GridGenerator : MonoBehaviour {

public int width;
public int height;


public Transform BoardGenerator;
public Transform cell;


// Use this for initialization
void Start () {
	GenerateGrid();
}

void GenerateGrid() {
	for (int x = 0; x < height; x++) {
		for (int y = 0; y < width; y++) {
			BoardGenerator = Instantiate (cell.gameObject).transform;
			BoardGenerator.parent = transform;
			BoardGenerator.position = new Vector3(88 * x, 88 * y, 0);

		
		}

	}

}

}

Thankyou very much ! :slight_smile: @danivdwerf

try this:


using UnityEngine; 
using System.Collections;
public class GridGenerator : MonoBehaviour 
{
     private int width;
     private int height;
     private GameObject BoardGenerator;
     [SerializeField]private Transform cell;
     private void Start () 
     {
         width= whatYouWant;
         height=whatYouWant;
          GenerateGrid();
     }
     private void GenerateGrid() 
     {
         for (int x = 0; x < height; x++) 
         {
              for (int y = 0; y < width; y++) 
             {
                  BoardGenerator = Instantiate (TheObjectYouWantToInstantiate,TheTransFormYouWant,Quaternion.Identity) as GameObject;              
             }
         }
     }
}

Looking at your code, you should also look up the terms OOP and SRP.

Good Luck!