- Home /
This question was
closed Jun 23, 2013 at 12:55 AM by
AlucardJay for the following reason:
Too subjective and argumentative : not enough information
How to make randomly generated levels from a set of prefabs?
I am a complete noob at coding so i don't really know what i am doing!
Comment
I have this snippet for you ;) (This is for 2D but you can modify it to work with 3D)
I must tell you that this is not a very good script, it have some limitations: It can only put a prefab in front of a prefab by a value you specify (this value is used for every prefab)
This thing does for some reason not seem to lag the game out when it is spawning the prefabs. I spawned over a million prefabs (it took a while XD) and the spawning did not lag the game, it only lags when there is too many objects
using UnityEngine;
using System.Collections;
public class levelGenerator : $$anonymous$$onoBehaviour {
public Transform[] prefabs;
public int currentX = 0;
public int NumberOfPieces = 500;
public int currentPieces = 0;
int prefabLength = 0;
int rndValue;
Random rnd = new Random();
// Use this for initialization
void Start () {
prefabLength = prefabs.Length;
currentX = 5;
}
// Update is called once per frame
void Update () {
if (!(currentPieces == NumberOfPieces))
{
currentPieces += 1;
rndValue = Random.Range(0, prefabLength);
Instantiate(prefabs[rndValue], new Vector3(currentX, 0, 0), Quaternion.identity);
currentX += 5;
}
}
}