Game is halting for a fraction of second

I am working on 3D race game. I am reusing the same track by placing a trigger in my track and When a player enter into the trigger I am generating the next part of the track. Everything goes fine but the Issue is, the game is halting for a fraction of second (I believe it is taking some time to build/render the next part of the track). Please help me how to solve this ?
Here is the code I have used

var plane_next : GameObject;
var plane : GameObject;
var treeCount : int = 0;
var tree_1_prefab : GameObject;
var tree_2_prefab : GameObject;
var gr1 : GameObject;
var gr2 : GameObject;
var gr3 : GameObject;
var hp1 : GameObject;
var hp2 : GameObject;
var hp3 : GameObject;
var sk1 : GameObject;
var sk2 : GameObject;
var sk3 : GameObject;


var trees = new Array();

private var randomFactor : int;

private var posGX : float[];
private var posGZ : float[];

private var posSX : float[];
private var posSZ : float[];


static var numberofPlanes : int = 1;

function Awake(){

    AssignGrPositions();
    AssignSkPositions();

   
}

function Start () {
   
}

function Update () {

}

function OnTriggerEnter(collision : Collider){
var tree : GameObject;
var index : int;
var zPosition : int;

    if(collision.transform.name == "character")
    {
       
        if ((collision.transform.position.z +50) > (numberofPlanes)*20){
           
           
            zPosition = (numberofPlanes*20)-10;
            Instantiate(plane_next, Vector3(0,1,zPosition), transform.rotation);
           
       
           
            index = Random.Range(0,2);
            gr1.transform.position = Vector3(posGX[index],gr1.transform.position.y,posGZ[index]+zPosition-20);
       
            index = Random.Range(3,6);
            gr2.transform.position = Vector3(posGX[index],gr2.transform.position.y,posGZ[index]+zPosition-20);
       
       
           
            index = Random.Range(0,2);
            hp1.transform.position = Vector3(posGX[index]+1,hp1.transform.position.y,posGZ[index]+1+zPosition-20);
       
            index = Random.Range(3,6);
            hp2.transform.position = Vector3(posGX[index]+1,hp1.transform.position.y,posGZ[index]+1+zPosition-20);
       
            index = Random.Range(0,10);
            sk1.transform.position = Vector3(posSX[index],2.08,posSZ[index]+zPosition);
            Debug.Log("SK1" + posSX[index]+ "***" + posSZ[index]);
            index = Random.Range(0,10);
            sk2.transform.position = Vector3(posSX[index],1.5,posSZ[index]+zPosition);
            Debug.Log("SK2" + posSX[index]+ "***" + posSZ[index]);
       
           
            numberofPlanes = numberofPlanes + 1;
           
       
        }
           
    }
}

function AssignGrPositions(){

    posGX = new float[11];
    posGZ = new float[11];
   
    posGX[0] = -2.860126;
    posGX[1] = 0.4432163;
    posGX[2] = 3.586888;
    posGX[3] = -1.033587;
    posGX[4] = 1.438848;
    posGX[5] = 4;
    posGX[6] = -3.919387;
    posGX[7] = -2.827086;
    posGX[8] = 0.5652422;
    posGX[9] = -4.46;
    posGX[10] = -2.741195;
   
    posGZ[0] = 0.3724308;
    posGZ[1] = -3.636194;
    posGZ[2] = -3.332674;
    posGZ[3] = 1.157265;
    posGZ[4] = 1.257332;
    posGZ[5] = 0.45;
    posGZ[6] = 2.967946;
    posGZ[7] = 3.74335;
    posGZ[8] = 4.047175;
    posGZ[9] = 2.183958;
    posGZ[10] = -1.47817;
   
   
}

function AssignSkPositions(){

    posSX = new float[11];
    posSZ = new float[11];
   
    posSX[0] = -4.54;
    posSX[1] = 2.31;
    posSX[2] = 8.66;
    posSX[3] = -6.81;
    posSX[4] = 7.36;
    posSX[5] = 1.73;
    posSX[6] = 1.73;
    posSX[7] = -0.3;
    posSX[8] = -8.85;
    posSX[9] = 6.85;
    posSX[10] = -2.741195;
   
    posSZ[0] = -28.24;
    posSZ[1] = -26.81;
    posSZ[2] = -26.33;
    posSZ[3] = -35.15;
    posSZ[4] = -36.60;
    posSZ[5] = -32.33;
    posSZ[6] = -28.20;
    posSZ[7] = -20.76;
    posSZ[8] = -20.76;
    posSZ[9] = -20.76;
    posSZ[10] = -1.47817;
   
   
}

You’ll run into trouble quickly doing too much in a single frame, especially like creating many new game objects. This needs to be spread over several frames with coroutines.

You’re up against, possible random garbage collection times + allocating huge chunks of memory + many calculations when generating. You also want reduce dynamic allocation whenever possible, when it is not possible, you want to specify the size ahead of time (like dictionaries/arrays) if you don’t they could end up resizing their contents (aka, copying entire contents to whole new sections in memory, which takes time and grows garbage faster)… and this may even be happening several times during a single function call if you’re not careful.

If the time it takes is not mission critical (like loading in distant objects certainly is not), throw it in a coroutine and spread the processing over several frames.

Small note, Debug calls themselves can cause a great deal of lag, but doubtfully the major hiccups you are experiencing, it is almost certainly memory or way too much processing in a single frame (if it’s noticeable, it’s already needing dozens of frames more than it has)