Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Following the complete-guide-to-procedural-level-generation-in-unity SeralizeField not getting input

Discussion in 'Getting Started' started by crs117, Aug 20, 2020.

  1. crs117

    crs117

    Joined:
    Oct 24, 2016
    Posts:
    4
    I somewhat new to Unity but have been programming for years (lots of C#). I am following the complete-guide-to-procedural-level-generation-in-unity and while most [SerializeField]s seem to get the input correctly from the inspectors I seem to be running into issues in part 2. Specifically the heightTerrainTypes and heatTerrainTypes. The fields show up in the inspector for the component. I have filled them out as indicated in the tutorial but when I run the debugger I keep getting this error.

    IndexOutOfRangeException: Index was outside the bounds of the array.
    TileGeneration.ChooseTerrainType (System.Single noise, TerrainType[] terrainTypes) (at Assets/TileGeneration.cs:184)
    TileGeneration.BuildTexture (System.Single[,] heightMap, TerrainType[] terrainTypes, TerrainType[,] chosenTerrainTypes) (at Assets/TileGeneration.cs:153)
    TileGeneration.GenerateTile (System.Single centerVertexZ, System.Single maxDistanceZ) (at Assets/TileGeneration.cs:115)
    LevelGeneration.GenerateMap () (at Assets/LevelGeneration.cs:38)
    LevelGeneration.Start () (at Assets/LevelGeneration.cs:20)​


    When I run the debugger I see that the value of these fields show as empty. ie {TerrainType[0]} even though i clearly have multiple values defined in the inspector and other values from the inspector are showing up properly. I have not intentionally skipped any steps.

    I know I am going to feel really stupid when I find the solution but I am currently dumbfounded as to why the values provided in the inspector are not getting serialized as inputs to the component.

    I apologize if I am not using the proper terms or if i need to provide any other information, but if somebody could enlighten me I would be really grateful.

    Thanks for any help and Unity is a great tool. =)

    PS I did some VR tests with Unity a couple of years ago and never seemed to run into this issue and the issue here seems to be to be random after double and triple checking everything.
     
    Last edited: Aug 20, 2020
  2. crs117

    crs117

    Joined:
    Oct 24, 2016
    Posts:
    4
    Discovered the issue I was having after taking a small break. I was editing the direct Level Tile object as opposed to the prefabbed instance. I am probably not using the proper terms, but I will try to explain.

    The tutorial creates a basic Level Tile object from which initial edits are made directly to that singular object. While still on the first tutorial it describes how to make a prefab of the Level Tile in order to seamlessly join tiles of that object in order to create a massive world.

    What I missed when starting the tutorial for part 2 today was that editing the Level Tile object directly does not translate to the prefab. Instead when the prefab (listed as a clone of the Level Tile in the Scene after being generated) has its own set of values in the inspector. When a prefab of an object is created, a '>' appears on the prefabbed object in the Scene that allows you to edit the prefab settings which will be the ones that are used when Unity is run. The solution was to click on the '>' and edit the values in the Inspector there.

    Oh the joys of learning a new tool. I am sure to many folks this is beginner stuff, and I am sure I will run into many other similar obstacles but this was not completely obvious to me even though the solution was quite simple. Add this to my things to pay attention to when creating dynamic objects.

    As I stated I am not new to coding or game programming, but learning Unity, like Blender or any other tool, requires experience at best or tenacity when nothing else seems to help.

    I hope this may help somebody else. Have fun coding =).
     
    Last edited: Aug 20, 2020