The question is answered, right answer was accepted
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. :)
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);
}
}
}
}
Answer by danivdwerf · Oct 20, 2016 at 01:54 PM
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!
Answer by janicevelarde · Oct 20, 2016 at 02:30 PM
Thankyou very much ! :) @danivdwerf
@anicevelarde you should close the question so people with the same issues know the answer works
Follow this Question
Related Questions
Instantiated prefab has a z-axis of 90 0 Answers
Instantiated prefab trying to keep weird Y position 0 Answers
How to store a position that is moving to create a prefab to spawn at the exact position 1 Answer
Instantiate prefab if no prefab exists at location 1 Answer
Convert Local Position To Global on a Child Object 0 Answers