Question by
$$anonymous$$ · May 27, 2016 at 04:54 PM ·
scripting probleminstantiate prefab
Instantiating prefab in a grid help
Hello, I've been having an issue with this bit of code I wrote. It generates the prefabs onto a grid-like pattern, but it stops once it has generated a certain amount, and I cannot get it to generate any more or less. Here is the code:
using UnityEngine;
using System.Collections;
public class GroundGenerator : MonoBehaviour {
public GameObject[] grass;
public float width;
public float length;
void Start () {
StartCoroutine (Generate ());
}
public IEnumerator Generate () {
for (int x = 0; x <= width; x++) {
for (int z = 0; z <= length; z++) {
Vector3 newLoc = new Vector3 (width * x + 1, 0.0f, length * z + 1);
Instantiate (grass[UnityEngine.Random.Range(0,3)], newLoc, Quaternion.identity);
yield return null;
}
}
}
}
The grass prefab is 3.2 lengths long in the X and Z plane, so I have my width and length set to 3.2. Thanks for the help!
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612081632im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiate And move the GameObject to the click position from another position. 1 Answer
turret shoot in direction and hit my health (scripts attached,i need to know how to put it together) 0 Answers
script add prefab instances to empty gameobject 1 Answer
Assign variables on Instantiation? 0 Answers
Spawning a Ball going forward every 5 seconds error, help! 2 Answers