- Home /
Unity crashes when I change values on inspector
Hi guys. I'm having a hard time trying to find my error on this and I'd like your help. Here's my problem:
I have a script (and it only happens with that specific script) that generates a terrain. I have an editor script that has a button "Generate" so that I can test it in the scene view. The problem is that I can generate this map once but every time the mesh is already created Unity crashes if I try to change any value in the inspector of the object. That's weird because I was not facing that problem before the last Unity update. I reinstalled Unity, I turned my PC off and the problem persists. And since Unity doesn't give any errors I imagined that the problem was some infinite loop that I had, but that's weird too because the code runs at least once, demonstrating that it doesn't have one.
Here is my terrain generation code:
using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEditor;
public class MapGeneratorInfinite : MonoBehaviour {
public enum DrawQuality{VeryLow, Low, Medium, High}
public DrawQuality drawQuality;
public bool isTerrace;
public float terraceDivision = 100;
[HideInInspector]
public float heightMultiplier;
public AnimationCurve heightCurve;
[HideInInspector]
public int chunkSize;
[HideInInspector]
public int levelOfDetail;
[HideInInspector]
public float scale;
public int octaves;
[Range(0,1)]
public float persistance;
public float lacunarity;
public int seed;
public Vector2 offset;
public TerrainType[] regions;
public bool useFallout;
public AnimationCurve falloutCurve;
public bool invertFalloutCurve;
public bool useCircularMap;
public bool invertCircularMap;
public bool generateRivers;
[Range(0,1)]
public float seaLevel;
[Range(0,1)]
public float spawnHeight;
[Range(0,30)]
public int numberOfRivers;
public bool autoUpdate;
public GameObject water;
[HideInInspector]
public VertexTile[,] tileMap;
[HideInInspector]
public float[,] noiseMap;
[HideInInspector]
public Color[] colourMap;
float[,] falloutMap;
float[,] circularMap;
void Awake(){
GenerateMap ();
}
public void GenerateMap(){
if (drawQuality == DrawQuality.VeryLow) {
chunkSize = 241;
levelOfDetail = 1;
scale = 240;
heightMultiplier = 24;
water.transform.localScale = new Vector3 (24, 0, 24);
}else if (drawQuality == DrawQuality.Low) {
chunkSize = 481;
levelOfDetail = 2;
scale = 480;
heightMultiplier = 48;
water.transform.localScale = new Vector3 (48, 0, 48);
}else if (drawQuality == DrawQuality.Medium) {
chunkSize = 961;
levelOfDetail = 3;
scale = 960;
heightMultiplier = 96;
water.transform.localScale = new Vector3 (96, 0, 96);
}else if (drawQuality == DrawQuality.High) {
chunkSize = 1921;
levelOfDetail = 4;
scale = 1920;
heightMultiplier = 192;
water.transform.localScale = new Vector3 (192, 0, 192);
}
water.transform.position = new Vector3(0, heightCurve.Evaluate (seaLevel) * heightMultiplier, 0);
//Gera terreno
noiseMap = Noise.GenerateNoiseMap (chunkSize, chunkSize, seed, scale, octaves, persistance, lacunarity, offset);
//Aplica fallout map
if (useFallout) {
noiseMap = ApplyFalloutMap (noiseMap);
}
//Aplica circular map
if (useCircularMap) {
noiseMap = ApplyCircularMap (noiseMap);
}
//Aplica terrace map
if (isTerrace) {
noiseMap = ApplyTerraceMap (noiseMap, terraceDivision);
}
//Cria Tilemap
tileMap = new VertexTile[noiseMap.GetLength(0), noiseMap.GetLength(0)];
for (int y = 0; y < noiseMap.GetLength(0); y ++) {
EditorUtility.DisplayProgressBar ("Generating tilemap: " + (y + 1), "Loading", (float)y / chunkSize);
for (int x = 0; x < noiseMap.GetLength(0); x ++) {
tileMap[x,y] = new VertexTile(noiseMap [x, y]);
}
}
EditorUtility.ClearProgressBar ();
//Pinta o mapa
colourMap = new Color[chunkSize * chunkSize];
for (int y = 0; y < chunkSize; y++) {
for (int x = 0; x < chunkSize; x++) {
float currentHeight = noiseMap [x, y];
for (int i = 0; i < regions.Length; i++) {
if (currentHeight <= regions .height) {
colourMap [y * chunkSize + x] = regions .color;
break;
}
}
}
}
//Gera rios
if (generateRivers) {
for (int i = 0; i < numberOfRivers; i++) {
EditorUtility.DisplayProgressBar ("Generating rivers: " + (i + 1), "Loading", (float)i / numberOfRivers);
bool generated;
AddRiver (out generated);
if (!generated) {
i--;
}
}
EditorUtility.ClearProgressBar ();
}
//Exibe resultado na tela
MapDisplay display = FindObjectOfType<MapDisplay> ();
display.DrawMesh (MeshGenerator.GenerateTerrainMeshInfinite (noiseMap, heightMultiplier, heightCurve, levelOfDetail), TextureGenerator.TextureFromColourMap (colourMap, chunkSize, chunkSize));
}
//Método chamado a cada alteração em variável
void OnValidate(){
if (octaves < 0) {
octaves = 0;
}
if (lacunarity < 1) {
lacunarity = 1;
}
if (scale < 1) {
scale = 1;
}
}
[System.Serializable]
public struct TerrainType
{
public string name;
public float height;
public Color color;
}
public void AddRiver(out bool generated){
generated = false;
colourMap = RiverGenerator.GenerateRiver (noiseMap, seaLevel, spawnHeight, colourMap, out generated);
}
public float[,] ApplyFalloutMap(float[,] heightMap){
float[,] returnMap = new float[heightMap.GetLength(0),heightMap.GetLength(0)];
falloutMap = FalloutNoise.GenerateFalloutMap (heightMap.GetLength(0), falloutCurve, invertFalloutCurve);
for (int y = 0; y < heightMap.GetLength(0); y++) {
for (int x = 0; x < heightMap.GetLength(0); x++) {
returnMap [x, y] = Mathf.Clamp01 (heightMap [x, y] - falloutMap[x,y]);
}
}
return returnMap;
}
public float[,] ApplyCircularMap(float[,] heightMap){
float[,] returnMap = new float[heightMap.GetLength(0),heightMap.GetLength(0)];
circularMap = CircularNoise.GenerateCircularNoise (heightMap.GetLength(0), invertCircularMap);
for (int y = 0; y < heightMap.GetLength(0); y++) {
for (int x = 0; x < heightMap.GetLength(0); x++) {
returnMap [x, y] = Mathf.Clamp01 (heightMap [x, y] - circularMap[x,y]);
}
}
return returnMap;
}
public float[,] ApplyTerraceMap(float[,] heightMap, float division){
float[,] returnMap = new float[heightMap.GetLength(0), heightMap.GetLength(0)];
for (int y = 0; y < heightMap.GetLength(0); y++) {
for (int x = 0; x < heightMap.GetLength(0); x++) {
float tempHeight = 0;
tempHeight = (Mathf.RoundToInt (heightMap [x, y] * division)) / division;
tempHeight = Mathf.Clamp01 (tempHeight);
returnMap [x, y] = tempHeight;
}
}
return returnMap;
}
}
And here is my Editor script:
using UnityEngine; using System.Collections; using UnityEditor;
[CustomEditor(typeof(MapGeneratorInfinite))] public class MapGeneratorInfiniteEditor : Editor {
public override void OnInspectorGUI(){
MapGeneratorInfinite mapGen = (MapGeneratorInfinite)target;
if (DrawDefaultInspector()) {
if (mapGen.autoUpdate) {
mapGen.GenerateMap ();
}
}
if (GUILayout.Button("Generate")) {
mapGen.GenerateMap ();
}
}
}
I'd be glad if someone could spot the problem. If the other functions are required, please let me know. Thanks.