- Home /
Question by
davidflynn2 · Jul 15, 2013 at 05:48 PM ·
c#spawnworldblocks
World Spawn Layer under current Layer
I have the following script spawning my parts of my world. I would like to add another layer to this shape just bellow the current one just not sure how to do it. Here is my script so far.
using UnityEngine;
using System.Collections;
public class WorldSpawn : MonoBehaviour {
public GameObject block1;
int worldWidth = 6;
int worldHeight = 6;
float spawnSpeed = 0;
void Start ()
{
StartCoroutine(CreateWorld());
}
IEnumerator CreateWorld (){
for(int x =0; x<worldWidth; x+=1) {
yield return new WaitForSeconds(spawnSpeed);
for(int z =0; z<worldHeight; z+=1) {
yield return new WaitForSeconds(spawnSpeed);
GameObject block = Instantiate(block1,block1.transform.position, block1.transform.rotation)as GameObject;
block.transform.localPosition = new Vector3(x, 0, z);
}
}
}
}
Comment
Best Answer
Answer by iwaldrop · Jul 15, 2013 at 08:18 PM
It seems like you need a 'worldDepth' variable and the nesting of another for loop.
for (x)
for (z)
for (y)
// instantiate blocks
Ok so this is the code I come up with from your suggestion the only thing I still need to change is I need the Y to use a different block.
using UnityEngine;
using System.Collections;
public class WorldSpawn : $$anonymous$$onoBehaviour {
public GameObject block1;
public GameObject block2;
int worldWidth = 6;
int worldHeight = 6;
int worldThickness = 3;
float spawnSpeed = 0;
void Start ()
{
StartCoroutine(CreateWorld());
}
IEnumerator CreateWorld (){
for(int x =0; x<worldWidth; x+=1) {
yield return new WaitForSeconds(spawnSpeed);
for(int z =0; z<worldHeight; z+=1) {
yield return new WaitForSeconds(spawnSpeed);
for(int y =0; y<worldThickness; y+=1) {
yield return new WaitForSeconds(spawnSpeed);
GameObject block = Instantiate(block1,block1.transform.position, block1.transform.rotation)as GameObject;
block.transform.localPosition = new Vector3(x, y, z);
}
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613110147im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Why Does My GameObject Not Spawn Anymore? 2 Answers
Spawn with rotation of 90 on x axis. 1 Answer
Minecraft Bedrock help 3 Answers
Adding Delay to a for loop in C# 1 Answer