- Home /
Question by
beau101023 · May 19, 2015 at 10:09 AM ·
terrainloopgenerationgenerator
Code Loops Indefinitely
I am coding a procedural terrain generator for a project, but for some reason my nested for loops keep repeating indefinitely.
using UnityEngine;
using System.Collections;
public class Terrain_Generator : MonoBehaviour
{
public GameObject Target_Obj;
public int width;
int iteration;
public int length;
public int height;
int prevPos;
void Start ()
{
}
// Update is called once per frame
void Update ()
{
for (int j=0; j<width; j = j + 1)
{
for (int i=0; i<length; i = i + 1)
{
int horizPos = Random.Range (prevPos + height, prevPos - height * 2);
Object.Instantiate (Target_Obj, new Vector3 (j, horizPos, i), Quaternion.identity);
prevPos = horizPos;
}
}
}
}
Comment
Best Answer
Answer by Graham-Dunnett · May 19, 2015 at 10:10 AM
Your loops are inside Update(). That function is called every frame, so your loops run every frame. If you don't want this, move the code into Start().
Your answer
Follow this Question
Related Questions
Terrain Generator Producing Completely Flat Results. 1 Answer
Stratum (layered) terrain generation 2 Answers
Problem with Terrain.SampleHeight 0 Answers
Block Terrain Generator 5 Answers
Generation Problem c# 0 Answers