Code not working.. What'd I miss?

Hello everyone,

I’ve been going over this problem lots of times.
Checked if “worldPos” is correct,
got feedback wether the terrain was too steep or water (only steepness triggered, but it works),
tried to set details[x,y] to -1 instead of 0…

No luck. It always puts details on the chosen terrain, wether my bool returns true or false.

If “density” is set to 0, it clears the terrain as it should. But the condition is ignored.
Either that or I completely overlooked something obvious. I hope you see my mistake and are
willing to point it out to me…

Thanks in advance. Now here’s the code.
A seperate monobehavior script references it.

using UnityEngine;
using System.Collections;

[System.Serializable]
public class MassGrass
{   
    [Range(0, 32)] public int density;
    [Range(0,359)] public int maxAngle;
    public bool               start;

    public void Process(Terrain map)
    {
        // Vars
        TerrainData data = map.terrainData;

        // Fill Layers
        for (int i = 0; i < data.detailPrototypes.Length; i++)
        {
            // Fill Tiles
            int[,] details = new int[data.detailWidth, data.detailHeight];
            for (int x = 0; x < data.detailWidth; x++)
            {
                for (int y = 0; y < data.detailHeight; y++)
                {
                    details[x, y] = 0;
                    if (IsValid(x, y, data))
                    {
                        details[x, y] = density;
                    }
                }
            }
            // Apply to Layer
            data.SetDetailLayer(0, 0, i, details);
        }
      
        // Apply to Map
        map.Flush();
    }

    bool IsValid(float x, float y, TerrainData mapData)
    {
        // World Position
        Vector3 worldPos = Vector3.zero;
        worldPos.x = (x / mapData.detailWidth) * mapData.size.x;
        worldPos.z = (y / mapData.detailHeight) * mapData.size.z;

        // Raycast
        RaycastHit hit = new RaycastHit();
        if (Physics.Raycast(worldPos + (Vector3.up * 100), Vector3.down, out hit, 200))
        {
            if (hit.collider.gameObject.layer != 9)
            {
                return false;
            }
            else if (Vector3.Angle(hit.normal, Vector3.up) > maxAngle)
            {
                return false;
            }
        }
        return true;
    }
}

Where is the problem? Does IsValid actually return true and false?

Yes, it returns false if either the raycast didn’t hit a ground layer or if the hit normals return an angle higher than maxAngle.
Other than that it returns true. The problem is that this isn’t actually applied when using the bool to check wether to use 0 or the preset density for these details-layers.