Search code examples
unity-game-engineunityscriptperlin-noise

Invalid IL code error in Unity3D


I'm trying to use perlin noise to generate terain using Unity3D (version 5.0.2f1) but I get this error:

InvalidProgramException: Invalid IL code in TerrainGenerator:GenerateFloor (): IL_0045: call      0x0a00000c


TerrainGenerator.Generate () (at Assets/TerrainGenerator.js:36)
TerrainGenerator.Start () (at Assets/TerrainGenerator.js:23)

Code:

function GenerateFloor(){
    print("The function was called");
    if(!(seed > 0)){
        Debug.LogError("Seed not valid. Seed: " + seed + " .");
        seed = Random.Range(0, 1000000000000000);
        Debug.LogError("Generated new seed. Seed: " + seed + ".");
    }
    for(var i = 0; i < heightMap.length; i++){
        if(currentX == Math.Sqrt(size)){
            currentX = 0;
            currentZ++;
        }
        else if(currentX > Math.Sqrt(size)) Debug.LogError("How did this happen?! currentX = " + currentX + " size = " + size + " .");
        var height = Mathf.PerlinNoise(currentX, currentZ);
        heightMap[currentX * currentZ] = new Vector3(currentX, height, currentZ);
        print("For loop worked");
        //yield;
    }
}

Solution

  • Your param 1000000000000000 in Random.Range is excessively beyond maximum. Maybe you want int.MaxValue ? which would be 2147483647

    Interesting it caused such a compiler error with IL2CPP !