Destroy a Cloned Cube

It wont let me destoy my cloned cube. After it clones and respawns (Im using an Instantiated prefab) and the player walks on it it doesn’t get destroyed and gives tons of points if you stay on it. I made it to where the cloned version has the same name as the original but it still wont work, its suppose to destroy once the player walks on it. and again the point system still works the cloned object just won’t destroy. How do i fix this? Heres my code for it:

using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CollisionPlayerOne : MonoBehaviour
 {
     
     public GameObject ObjPrefab;
     
     public pOneMovement PlayerOne;
     
     void OnCollisionEnter(Collision col)
     {
         if (col.gameObject.name == "playerOneGoal")
         {
             ScoreScript.scoreValue += 1; 
             
             Destroy (col.gameObject);
             
             spawnPrefab();
 
         }
 
     }
     
     private void spawnPrefab() 
     {
             
         ObjPrefab = Instantiate(ObjPrefab) as GameObject;
         ObjPrefab.transform.position = new Vector3(-3.63f, 0.6f, 1.24f);
                 
         ObjPrefab.name = "playerOneGoal";
     }
 
     
 }

As much I see you destroy it, but you keep respawning it on the same spot.

Try removing the spawnPrefab(); and the object should stay destroyed.

If you want to keep the same functionality you should adjust the spawn to be far from the player.

You can also try to change your spawnPrefab method like this:

public GameObject ObjPrefab;

private void spawnPrefab() 
{
    //create a position
    var newPosition = transform.position;
    
    //we do this check to avoid spawning the object above the player
    while (Math.Abs(newPosition.x - transform.position.x) > .5f)
    {
        //create a position, the values should be the size of your grid. I set them as (0f, 10f), but they could be (-5f, 5f)
        newPosition = new Vector3(Random.Range(0f, 10.0f), 0.6f, Random.Range(0f, 10.0f));
    }
    
    //spawn the new item on the position
    Instantiate(ObjPrefab, newPosition, Quaternion.identity).name = "playerOneGoal";
}

It seems that you are spawning the object in the same spot and it then enters a collision with the player and then gets destroyed over and over and over again. Is it getting spawned in the same spot?? If so, what is the behavior that you are looking for? Maybe wait for a given amount of time before respawning?? Or spawn in a different position??