on collisionEnter infinite prefabs intantiated and dont stop

The problem with this code is when the third prefab collides with the prefab had fallen before it. it produces infinite prefabs and unity goes into not responding state. this is my code. i am new to the unity please guide me.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class onCubeClick : MonoBehaviour {

 private Animator cubeAnim;
 public GameObject spawnCube;
 //private int score;

 private void Awake()
 {
     cubeAnim = GetComponent<Animator>();
 }
 private void OnMouseDown()
 {
     gameObject.GetComponent<Rigidbody>().useGravity = true;
     cubeAnim.enabled = false;
 }

 private void OnCollisionEnter(Collision collision)
 {
     if (collision.gameObject.name == "table" || collision.gameObject.name == "Cube(Clone)" )
       {
        spawnCube = Instantiate(Resources.Load("Prefabs/Cube"), new Vector3(288, 4, 208), Quaternion.identity) as GameObject;
        }
 }

 // Use this for initialization
 void Start () {
 }
 
 // Update is called once per frame
 void Update () {
     
 }
}

The problem is the conditional in if (collision.gameObject.name == "table" || collision.gameObject.name == "Cube(Clone)" )

When the cube is spawned, it triggers a collision enter event because it interacts with the collider you have this script on. When that cube is spawned, it spawns another one, ad infinitum. Remove the collision.gameObject.name == "Cube(Clone)" condition, and it’ll run only once.

use collision.gameObject.tag==“” instead of collision.gameObject.name=“”, give a tag name to you prefab and collision by that not by name

@wasimsahi
Infinite because prefab spawned and collide in the same place and it doesn’t wait the gravity to fall down.

Try this

private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.name == "Plane" || collision.gameObject.name == (Table.CubeCount-1).ToString())
        {
            /* add Y position base and CubeCount to avoid collision in the same position simulteanously */
            var spawnCube = Instantiate(Cube, new Vector3(0, (4 + Table.CubeCount*2), 0), Quaternion.identity) as GameObject;
            spawnCube.name = Table.CubeCount++.ToString();
            /* because you want usGgravity OnMouseDown*/
            spawnCube.GetComponent<Rigidbody>().useGravity = false;
        }
    }

Add this to table object
Table.cs

using UnityEngine;
using System.Collections;

public class Table : MonoBehaviour {

    public static int CubeCount;
}