So, I just started this new 2D project and I made a script to generate some terrain using tiles. I made some prefabs and tried instantiating them using this code:
using UnityEngine;
using System.Collections;
public class TerrainGenerator : MonoBehaviour {
public GameObject[ ] grassTiles;
/*
public GameObject[ ] dirtTiles;
public GameObject[ ] stoneTiles;
public GameObject[ ] sandTiles;
public GameObject[ ] waterTiles;
public GameObject[ ] wallTiles;
*/
void GenerateTerrain (GameObject[ ] tileArray, int terrainHeight, int terrainWidth) {
for (int y = 0; y < terrainHeight; y++) {
for (int x = 0; x < terrainHeight; x++) {
GameObject tileChoice = tileArray[Random.Range(0, tileArray.Length)];
Instantiate(tileChoice, new Vector3(x, y, 0), Quaternion.identity);
}
}
}
void Awake() {
GenerateTerrain(grassTiles, 10, 10);
}
}
Can anyone come up with a solution? Thanks!