How do I change the position of a prefab after each spawn?

After a prefab spawns and is destroyed, I want to change it so that when I spawn the prefab again, it spawns at a position 10.0 z units higher than the previous one. I’m stuck and this code seems completely wrong. I want to keep the position of the x and the position of the y the same throughout the process. This is what I have so far:

public class InstantiateFloor : MonoBehaviour {

 public GameObject Spawner;
public float PositionofQuad = 10.0f;


void FixedUpdate(){
	CreatePrefab ();
}

void CreatePrefab ()
{
InstantiationTimer -= Time.deltaTime;
if (InstantiationTimer <= 0)
{
		Instantiate(Spawner, new Vector3(0.0f, 0.0f, transform.position.z + 10.0f), Quaternion.identity);
	InstantiationTimer = 1.0f;

		float PositionofQuad = +10.0f;

}
}

}

You are not showing all relevant code, so I can only describe the general idea, and you’ll have to adjust it to your code.

float previousZ; the z value when the prefab is destroyed

//example of code, since you have not provided it
void DestroyPrefab(){

//your existing code

previousZ = transform.position.z; //store the value of Z when the prefab gets destroyed

}

 void CreatePrefab ()
 {
 InstantiationTimer -= Time.deltaTime;
 if (InstantiationTimer <= 0)
 {
         Instantiate(Spawner, new Vector3(0.0f, 0.0f, previousZ + 10.0f), Quaternion.identity);
     InstantiationTimer = 1.0f;
         float PositionofQuad = +10.0f;  //I'm not sure why you need this... it's a different (local) variable, from the class variable of same name... and "= +" operand??? does this work as expected?
 }
 }

Well a couple things:

  1. I wouldn’t recommend using FixedUpdate to spawn objects, as it’s main use is for updating physics information, so Update would be better (That’s relative to fixed update, i still would recommend not using Update() to spawn objects, for what you’re doing, unless you have it under control)
  2. Use a For Loop to generate your floor, if its something that’s happening at the start only (Ignore this if there’s a gameplay reason for this)
  3. Look into object pooling if you need to be constantly spawning floors, that way you can reuse ones already instantiated, as the instantiation process is computationally expensive

Here’s an example of how you can use the for loop:

for(int i = 0; i < 10; i++)
{
      Instantiate(Spawner, Vector.forward * i, Quaternion.identity);
}