Game objects are getting instantiated at the same position.

So my player is moving along the Y axis downwards but not at a constant or predictable rate (meaning the player can even stop moving ). The camera is following the player so I decided to use it to set a location for spawning game objects along the y axis. I used a Yoffset to make it spawn objects ahead of the camera and a DifferenceInDistance variable to try and maintain a distance between game objects, however, I’m still having game objects appearing at the same position. Please Help thanks :slight_smile:

public class ObjectSpawnner : MonoBehaviour {
	
	public float Yoffset;


	public GameObject [] obstacles;

	public int NumberOfGameObjects;
	public float startTime;
	public float RepeatRate;

	public float DifferenceInDistance = 4;



	float YDistance = 0;


	// Use this for initialization
	void Start () {
		InvokeRepeating ("GenerateRandomObjects",startTime ,RepeatRate );
	
	}
   


	void GenerateRandomObjects(){
		float x =obstacles [0].transform.position.x;
		float y = GameObject.Find ("Main Camera").transform.position.y;
		int r = Random.Range (0,NumberOfGameObjects);

		YDistance += DifferenceInDistance;
		Debug.Log ("Y distance is " + YDistance );

		Instantiate (obstacles [r], new Vector3 (x, y - Yoffset - YDistance, 0), Quaternion.identity);
	}

}

So I ran your code as is and it works as I described, spawning cubes all the way downwards without any hickups.

The only fact that this could fail is that it constantly uses the Main Camera position.y and you said you were moving it, but then only if the camera goes up, since the offset extends down. I’d put the spawner on a gameobject that stands still and starts in the same position as the camera, OR you save the starting position of the camera and keep using that y instead