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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

cannot implicitly convert from float to float[,] error even though i put [,]??

Discussion in 'Scripting' started by Nimsy, Jul 18, 2018.

  1. Nimsy

    Nimsy

    Joined:
    Apr 2, 2018
    Posts:
    3
    Idk why, but in 'GenerateHeights' i said float[,] heights = new float(width, height); and as far as I know, that should work fine, but i get a compiler error that says these two things: Float does not contain a constructor that takes 2 arguments and Cannot implicitly convert from float to float[,].

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

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,419
    Use square brackets instead.
    Code (CSharp):
    1. float[,] heights = new float[width, height];
     
  3. Nimsy

    Nimsy

    Joined:
    Apr 2, 2018
    Posts:
    3
    Thank you so much! I knew I made a stupid mistake I just couldn't find it. Have a nice day :D