I am trying to create a piece of terrain when the sphere obj.z goes past the halfway point of my terrain obj.z and delete it when it goes past the camera.z Since there will be many terrains at once I created a list of terrains:
private List<nicoTerrain> terrains = new List<nicoTerrain>();
I instantiate 3 of them at Start()
void Start () {
terrains.Add(Instantiate(terrainOriginal));
spawnPos += new Vector3(0, 0, terrainOriginal.transform.localScale.z/1.1f);
terrains[terrains.Count - 1].transform.position = spawnPos;
//create 2 more.
terrains.Add(Instantiate(terrainOriginal));
spawnPos += new Vector3(0, -30, -terrainOriginal.transform.localScale.z);
terrains[terrains.Count - 1].transform.position = spawnPos;
//Last one Bois
terrains.Add(Instantiate(terrainOriginal));
spawnPos += new Vector3(0, -30, -terrainOriginal.transform.localScale.z);
terrains[terrains.Count - 1].transform.position = spawnPos;
Debug.Log("got to the end of start bois");
}
but I am having trouble with my Update:
void Update () {
sphereVel = sphere.GetComponent<Rigidbody>().velocity.z;
if (terrains[terrains.Count - 2].transform.position.z > sphere.transform.position.z && sphereVel > 1){
terrains.Add(Instantiate(terrainOriginal));
spawnPos += new Vector3(0, -30, terrainOriginal.transform.localScale.z * sphereVel * 0.9f);
terrains[terrains.Count - 1].transform.position = spawnPos;
}
else if(sphere.transform.position.z < terrains[terrains.Count - 2].transform.position.z - terrains[terrains.Count - 2].transform.localScale.z / 2)
{
terrains.Add(Instantiate(terrainOriginal));
spawnPos += new Vector3(0, -30, -terrainOriginal.transform.localScale.z);
terrains[terrains.Count - 1].transform.position = spawnPos;
}
if(terrains[0].transform.position.z > mainCamera.transform.position.z){
Destroy(terrains[0]);
terrains.Remove(terrains[0]);
}
Debug.Log("Sphere Velocity: " + sphereVel);
Debug.Log("Terrains Count: " + terrains.Count);
}
the destroy if statement is "not set to an instance of an object" and my first if statement is never called. My "greater than" and "less than" symbols are inversed because the sphere.z decreases as it moves.