Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PvTGreg · May 02, 2021 at 04:30 PM · bugbug-perhapsbug report

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();
         }
     }
 }
 

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HellsHand · May 02, 2021 at 05:57 PM 0
Share

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.

avatar image PvTGreg HellsHand · May 02, 2021 at 06:02 PM 0
Share

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

avatar image PvTGreg HellsHand · May 02, 2021 at 07:13 PM 0
Share

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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

135 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges