Why i'm getting warning “Terrain not hit, using default height !” in c# script?

In the editor in the scene i have Terrain, ThirdPersonController, Main Camera , Cube , GameObject(empty)

The Terrain tag is set to Terrain and the Layer is DynamicObjects And i also added a tag name Cube

On the empty GameObject i attached the script that clone objects. In this case cloning Cubes.

In the GameObject Inspector in the script i dragged the Cube to the Object To Clone. The Objects Starting Height is set to 0 The Extents are set to X = 250 Y = 20 Z = 250 Object Count 100 Object Size 20

The Terrain position is X = -250 Y = 0 Z = -250

Now when running the game it does cloning and show 100 cubes. But i keep getting a warning in the console window:

Terrain not hit, using default height ! 0x000000014151284B (Unity) StackWalker::GetCurrentCallstack

using System;
using UnityEngine;
using Random = UnityEngine.Random;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class CloneObjects : MonoBehaviour
{

   public GameObject ObjectToClone;
   public int objectsStartingHeight = 20;
   [HideInInspector]
   public GameObject[] clonedObjects;

   // for tracking properties change
   private Vector3 _extents;
   private int _objectCount;
   private float _objectSize;
   private List<int> randomNumbers = new List<int>();

   /// <summary>
   ///     How far to place spheres randomly.
   /// </summary>
   public Vector3 Extents;

   /// <summary>
   ///     How many spheres wanted.
   /// </summary>
   public int ObjectCount;
   public float ObjectSize;

   // Use this for initialization
   void Start()
   {

       rndNumbers();
       Clone();
       clonedObjects = GameObject.FindGameObjectsWithTag("Cube");
   }

   private void OnValidate()
   {
       // prevent wrong values to be entered
       Extents = new Vector3(Mathf.Max(0.0f, Extents.x), Mathf.Max(0.0f, Extents.y), Mathf.Max(0.0f, Extents.z));
       ObjectCount = Mathf.Max(0, ObjectCount);
       ObjectSize = Mathf.Max(0.0f, ObjectSize);
   }

   private void Reset()
   {
       Extents = new Vector3(250.0f, 20.0f, 250.0f);
       ObjectCount = 100;
       ObjectSize = 20.0f;
   }

   // Update is called once per frame
   void Update()
   {

   }

   private void Clone()
   {
       if (Extents == _extents && ObjectCount == _objectCount && Mathf.Approximately(ObjectSize, _objectSize))
           return;

       // cleanup
       var ShipsToDestroy = GameObject.FindGameObjectsWithTag("Cube");
       foreach (var t in ShipsToDestroy)
       {
           if (Application.isEditor)
           {
               DestroyImmediate(t);
           }
           else
           {
               Destroy(t);
           }
       }

       var withTag = GameObject.FindWithTag("Terrain");
       if (withTag == null)
           throw new InvalidOperationException("Terrain not found");

       for (var i = 0; i < ObjectCount; i++)
       {
           var o = Instantiate(ObjectToClone);
           o.tag = "Cube";
           o.transform.SetParent(base.gameObject.transform);
           o.transform.localScale = new Vector3(ObjectSize, ObjectSize, ObjectSize);

           // get random position
           var x = Random.Range(-Extents.x, Extents.x);
           var y = Extents.y; // sphere altitude relative to terrain below
           var z = Random.Range(-Extents.z, Extents.z);

           // now send a ray down terrain to adjust Y according terrain below
           var height = 10000.0f; // should be higher than highest terrain altitude
           var origin = new Vector3(x, height, z);
           var ray = new Ray(origin, Vector3.down);
           RaycastHit hit;
           var maxDistance = 20000.0f;
           var nameToLayer = LayerMask.NameToLayer("Terrain");
           var layerMask = 1 << nameToLayer;
           if (Physics.Raycast(ray, out hit, maxDistance, layerMask))
           {
               var distance = hit.distance;
               y = height - distance + y; // adjust
           }
           else
           {
               Debug.LogWarning("Terrain not hit, using default height !");
           }

           //o.transform.Rotate(0.0f,randomNumbers[i],0.0f);
           // place !
           o.transform.position = new Vector3(x, y + objectsStartingHeight, z);
       }

       _extents = Extents;
       _objectCount = ObjectCount;
       _objectSize = ObjectSize;
   }

   public void rndNumbers()
   {

   }
}

This is the part in the script with the RayCast and the LogWarning

if (Physics.Raycast(ray, out hit, maxDistance, layerMask))
           {
               var distance = hit.distance;
               y = height - distance + y; // adjust
           }
           else
           {
               Debug.LogWarning("Terrain not hit, using default height !");
           }

he cloned cube are in height 20 or 0 but in any case i’m getting the warning. How should i handle/fix the warning and why i’m getting it ?

Use Debug.Drawray to see what is happening.

By the looks of it though, you are setting a direction for the ray.