[Help] Playing game crashes Unity.

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!

You didn’t really tell us what your problem is so I’m not sure how to help.

That said, I did notice this:

for (int y = 0; y < terrainHeight; y++) {
for (int x = 0; x < terrainHeight; x++) {
...
}
}

which should probably be this:

for (int y = 0; y < terrainHeight; y++) {
for (int x = 0; x < terrainWidth; x++) {
...
}
}

but I can’t see that causing any serious problems, especially since you’re sending in a square map anyway (10x10)

1 Like