Spawn Objects to move toward the camera by timer

Hey guys I am quite new to Unity development. I am trying to learn how to spawn objects in a distance away from the camera and move them toward the camera on the Z axis.

I have created an empty GameObject and created a prefab to use as my moving object. I’d like it to spawn every few seconds so it looks like there are many objects coming toward the camera then destroy them once they pass the camera and no longer in view.

With this code I am able to spawn 1 object but it doesn’t move.

public GameObject obstacles;

void Start () {

Instantiate (obstacles, transform.position, transform.rotation);
}

void Update () {

obstacles.transform.position = new Vector3 (0, 0, -30);
}

Could someone help me out please? Thanks

In your Update Function, you are setting the obstacle’s position to a fixed location instead of incrementing the position by a speed. Instead of using

obstacles.transform.position = new Vector3 (0, 0, -30); 

Use

obstacles.transform.position += new Vector3 (0, 0, -30); 

This should change the obstacle’s position.