So I have this script which seems to crash Unity when called. There is no infinite loop or anything. It's acctually a pretty small loop. This is the script I'm refering to. It worked fine up until I added the part with more specific grass detection(the nested if statements).
using UnityEngine;
using System.Collections;
public class LevelMaker : MonoBehaviour {
public Texture2D level;
public GameObject[] grass;
public GameObject water;
private Color grassColor;
private Color waterColor;
void Start () {
grassColor = new Color(0,1,0);
waterColor = new Color(0,0,1);
Debug.Log ("class called");
for (int x = 0; x < level.width; x++) {
for (int y = 0; y < level.height; y++) {
Debug.Log("Instatiating");
if(level.GetPixel(x, y) == grassColor) {
if(level.GetPixel(x++, y) == waterColor) { //GRASS WITH WATER TO THE RIGHT
Instantiate(grass[1], new Vector3(x, y, 0), Quaternion.identity);
} else if (level.GetPixel(x, y--) == waterColor) { //GRASS WITH WATER AT THE BOTTOM
Instantiate(grass[2], new Vector3(x, y, 0), Quaternion.identity);
} else if (level.GetPixel(x--, y) == waterColor) { //GRASS WITH WATER TO THE LEFT
Instantiate(grass[3], new Vector3(x, y, 0), Quaternion.identity);
} else if (level.GetPixel(x, y++) == waterColor) { //GRASS WITH WATER AT THE TOP
Instantiate(grass[4], new Vector3(x, y, 0), Quaternion.identity);
} else if (level.GetPixel(x++, y++) == waterColor) { // GRASS WITH WATER UP RIGHT
Instantiate(grass[5], new Vector3(x, y, 0), Quaternion.identity);
} else if (level.GetPixel(x++, y--) == waterColor) { // GRASS WITH WATER DOWN RIGHT
Instantiate(grass[6], new Vector3(x, y, 0), Quaternion.identity);
} else if (level.GetPixel(x--, y--) == waterColor) { // GRASS WITH WATER DOWN LEFT
Instantiate(grass[7], new Vector3(x, y, 0), Quaternion.identity);
} else if (level.GetPixel(x++, y++) == waterColor) { // GRASS WITH WATER UP LEFT
Instantiate(grass[8], new Vector3(x, y, 0), Quaternion.identity);
} else { //GRASS WITH GRASS AT SIDES
Instantiate(grass[0], new Vector3(x, y, 0), Quaternion.identity);
}
} else if (level.GetPixel(x, y) == waterColor) {
Instantiate(water, new Vector3(x, y, 0), Quaternion.identity);
}
}
}
}
}
↧