Search Unity

Why doesn't this do anything when I hit play?

Discussion in 'Getting Started' started by Altissimus, May 19, 2019.

  1. Altissimus

    Altissimus

    Joined:
    May 11, 2015
    Posts:
    49
    This script is attached to a terrain in my hierarchy. There's nothing else in there (apart from camera, light source):

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class TerrainGenerator : MonoBehaviour
    5. {
    6.     public int width=256, height=256, depth = 20;
    7.     public float scale = 20f;
    8.  
    9.     void start()
    10.     {
    11.         print("Start.");
    12.         Terrain terrain = GetComponent<Terrain>();
    13.         terrain.terrainData = GenerateTerrain(terrain.terrainData);
    14.         print("Hello?");
    15.     }
    16.  
    17.     TerrainData GenerateTerrain (TerrainData terrainData)
    18.     {
    19.         terrainData.heightmapResolution = width + 1;
    20.         terrainData.size = new Vector3(width, depth,height);
    21.         terrainData.SetHeights(0,0, GenerateHeights());
    22.         print("Am I here");
    23.         return terrainData;
    24.        
    25.  
    26.  
    27.     }
    28.  
    29.     float[,] GenerateHeights()
    30.     {
    31.         float[,] heights = new float[width,height];
    32.         for (int x = 0; x<width; x++)
    33.         {
    34.             for (int y = 0; y < height; y++)
    35.             {
    36.                 heights[x,y] = CalculateHeight (x,y);
    37.             }
    38.         }
    39.         print("Am I here too");
    40.         return heights;
    41.  
    42.     }
    43.  
    44.     float CalculateHeight(int x, int y)
    45.     {
    46.         float xCoord = (float)x/width * scale;
    47.         float yCoord = (float)y/height * scale;
    48.         print("Am I here three");
    49.         return Mathf.PerlinNoise(xCoord, yCoord);
    50.     }
    51.  
    52. }
    53.  
     
  2. vampir26

    vampir26

    Joined:
    Mar 29, 2010
    Posts:
    108
    Try
    Code (CSharp):
    1. terrain.Flush();
    to apply changes.
     
  3. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    It's Start, with a capital S
     
    Joe-Censored likes this.