- Home /
Script only works if i enable/disable it in inspector
Hi, you can see this issue here https://recordit.co/c3Fl0PlPyV i can change the seed of my terrain in the scriptable object in the inspector fine but in runtime it does nothing unless i enable/disable the script. really not sure whats going on i have been trying to diagnose it for ages by removing everything and running each part seperatly but the only fix is enabling/disabling the script. this obviously isnt possible when i build the game. i have attatched the three main scripts but i am just really confused by this. the same thing happens if i try and change the values in the scriptable object in runtime the map disapears but if i enable/disable that script it works fine
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapGenerator : MonoBehaviour
{
//public enum DrawMode { NoiseMap,ColorMap,Mesh,FallOffMap}
//public DrawMode drawMode;
public TerrainData terrainData;
public TerrainTypes[] regions;
float[,] fallOffMap;
private void Awake()
{
fallOffMap = FallOffGenerator.GenerateFallOffMap(terrainData.mapWidth * terrainData.mapHeight);
}
private void Start()
{
}
public void DrawMapInEditor()
{
if (!Application.isPlaying)
{
MapData mapData = GenerateMapData();
MapDisplay display = FindObjectOfType<MapDisplay>();
//if (drawMode == DrawMode.NoiseMap)
//{
// display.DrawTexture(TextureGenerator.TextureFromHeightMap(mapData.heightMap));
//}
//else if (drawMode == DrawMode.ColorMap)
//{
// display.DrawTexture(TextureGenerator.TextureFromColorMap(mapData.colorMap, terrainData.mapWidth, terrainData.mapHeight));
//}
//else if (drawMode == DrawMode.Mesh)
//{
display.DrawMesh(MeshGenerator.GenerateTerrainMesh(mapData.heightMap, terrainData.meshHeightMultiplier, terrainData.meshHeightCurve), TextureGenerator.TextureFromColorMap(mapData.colorMap, terrainData.mapWidth, terrainData.mapHeight));
//}
//else if (drawMode == DrawMode.FallOffMap)
//{
// display.DrawTexture(TextureGenerator.TextureFromHeightMap(FallOffGenerator.GenerateFallOffMap(terrainData.mapWidth)));
//}
}
}
public void GenerateMapRuntime()
{
MapData mapData = GenerateMapData();
MapDisplay display = FindObjectOfType<MapDisplay>();
display.DrawMesh(MeshGenerator.GenerateTerrainMesh(mapData.heightMap, terrainData.meshHeightMultiplier, terrainData.meshHeightCurve), TextureGenerator.TextureFromColorMap(mapData.colorMap, terrainData.mapWidth, terrainData.mapHeight));
}
MapData GenerateMapData()
{
float[,] noiseMap = Noise.GenerateNoiseMap(terrainData.mapWidth, terrainData.mapHeight, terrainData.seed, terrainData.noiseScale, terrainData.octaves, terrainData.persistance, terrainData.lacunarity, terrainData.offset);
Color[] colorMap = new Color[terrainData.mapWidth * terrainData.mapHeight];
for(int y = 0; y < terrainData.mapHeight; y++)
{
for(int x = 0; x < terrainData.mapWidth; x++)
{
if (terrainData.useFallOff)
{
noiseMap[x, y] = Mathf.Clamp01( noiseMap[x, y] - fallOffMap[x,y]);
}
float currentHeight = noiseMap[x, y];
for(int i = 0; i < regions.Length; i++)
{
if(currentHeight <= regions[i].height)
{
colorMap[y * terrainData.mapWidth + x] = regions[i].color;
break;
}
}
}
}
return new MapData(noiseMap, colorMap);
}
void OnValuesUpdated()
{
if (!Application.isPlaying)
{
DrawMapInEditor();
}
else
{
GenerateMapRuntime();
}
}
private void OnValidate()
{
if(terrainData != null)
{
terrainData.OnValuesUpdated -= OnValuesUpdated;
terrainData.OnValuesUpdated += OnValuesUpdated;
}
fallOffMap = FallOffGenerator.GenerateFallOffMap(terrainData.mapWidth);
}
}
[System.Serializable]
public struct TerrainTypes
{
public string name;
public float height;
public Color color;
}
public struct MapData{
public float[,] heightMap;
public Color[] colorMap;
public MapData(float[,] heightMap,Color[] colorMap)
{
this.heightMap = heightMap;
this.colorMap = colorMap;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(TerrainData),true)]
public class UpdateableDataEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
TerrainData data = (TerrainData)target;
if (GUILayout.Button("Generate"))
{
data.NotifyOfUpdatedValues();
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu()]
public class TerrainData : ScriptableObject
{
public bool autoUpdate;
public int mapWidth;
public int mapHeight;
public float noiseScale;
public int octaves;
[Range(0, 1)]
public float persistance;
public float lacunarity;
public int seed;
public Vector2 offset;
public bool useFallOff;
public float meshHeightMultiplier;
public AnimationCurve meshHeightCurve;
private void OnValidate()
{
if (autoUpdate)
{
NotifyOfUpdatedValues();
}
if (mapWidth < 1)
{
mapWidth = 1;
}
if (mapHeight < 1)
{
mapHeight = 1;
}
if (lacunarity < 1)
{
lacunarity = 1;
}
if (octaves < 0)
{
octaves = 0;
}
}
public event System.Action OnValuesUpdated;
public void NotifyOfUpdatedValues()
{
if(OnValuesUpdated != null)
{
OnValuesUpdated();
}
}
}
One thing disabling/enabling your script is doing is basically calling Awake()
at a later time. What if you tried setting fallOffMap
in Start()
or a one time run in Update()
? This may not be ideal for you in the end build but it may lead you to what's going on.
Thanks for the reply. so i tried moving that to start and also calling it everytime i update the map but still nothing. but it has given me info. what i also noticed is the mesh is generated but it has no height values so im thinking it may be something to do with the animation curve im using for the height not being passed properly. gotta do some more digging
Been fiddling with it for an hour and now its not working in the editor but works fine at runtime. and i dont think i actually changed anything
Your answer
Follow this Question
Related Questions
Animation load bug 0 Answers
Variable value not changhing 3 Answers
TextMeshPro Object not updating in real time 0 Answers
Unity bug? Coroutine couldn't be started inactive game object 0 Answers
Unity Line Renderer drawing extra line to another point.. 0 Answers