How to make randomly created objects follow the camera ?

Hi everyone me and my friend are working on this game for like 1 week now. The concept is basically a ball falling down (and camera following the ball) while it’s trying to evade the piked platforms passing through the screen on its way down . We wrote this code for the piked platform for its movement and vanishing when it is out of the main camera and the platform is a prefab :
(in the code, “dikenli” means piked)

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

public class dikenli : MonoBehaviour
{
    public float speed = 10.0f;
    private Rigidbody2D rb;
    private Vector2 screenBounds;
   
    // Start is called before the first frame update
    void Start()
    {
        rb = this.GetComponent<Rigidbody2D>();
        rb.velocity = new Vector2(speed, 0);
        screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));

    }

    // Update is called once per frame
    void Update()
    {
        if (transform.position.x > screenBounds.x * 2)
            Destroy(this.gameObject);
    }
}

[ICODE][/ICODE]

And we wrote this code for creating the random piked platforms based on the piked platform prefab and inserted this code to main camera :

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


public class deployDikenli : MonoBehaviour
{
    public GameObject dikenliPrefab;
    public float respawnTime = 1.0f;
    private Vector2 screenBounds;
    // Start is called before the first frame update
    void Start()
    {
        screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width , Screen.height , Camera.main.transform.position.z));
        StartCoroutine(dikenliWave());
    }
    private void spawnEnemy()
    {
        GameObject a = Instantiate(dikenliPrefab) as GameObject;
        a.transform.position = new Vector2(screenBounds.x * -2, Random.Range(-screenBounds.y , screenBounds.y));
    }
    IEnumerator dikenliWave()
    {
        while (true)
        {
            yield return new WaitForSeconds(respawnTime);
            spawnEnemy();
         }  
     }  
}

When the game starts main prefab starts to pass the screen and generates a clon when it is destroyed. The cycle continues. But the piked platform clones stay generated in the area of initial position of the camera and don’t follow the main camera. We want the generated clones to follow the main camera and we want their generation area to follow the main camera. How can we accomplish this ? Thanks in advance.

Here’s some pictures of what it looks like :

The main prefab passing the screen
4446427--407383--Ekran Alıntasdasdasdasısı.PNG

Randomly generated clones staying in the initial camera area

To addition to all this i want to ask if there is any way to determine the distance between randomly generated objects so they can generate in specific area (or the area that follows the camera) only with the condition of previously determined distance intervals. Thanks again.

The camera is a gameObject, you can simply get it’s position and add it in the Spawnfunction where you set the position of the prefab. Get a reference like so:
[SerializeField]
private Transform cameraTransform;

Drag the camera object onto it in the inspector. Now you can use cameraTransform.position in your script and add that to the spwaned prefab. You could also just move the whole spawner Object based on the camera position.

To get the distance between two prefabs you can use:
float distance = Vector2.Distance(prefab1.transform.position, prefab2.transform.position);