Question by
epicchris38 · Nov 20, 2019 at 03:03 PM ·
c#2d gamegenerationdestroy object
This code is supposed to generate then delete platforms for my endless runner. Does not delete. Any idea for fix?
public class PlatformGen : MonoBehaviour { public GameObject Destroyt; public Transform generationpoint; public float distancebetween; private float platformWidth; public float distancemax; public float distancemin; public GameObject[] Platformbegin; private int platformselector; private float[] platformWidths; private GameObject cloneplatform; // Cannot pool as I will have too many prefabs in the future void Start() { Destroyt = GameObject.Find("Destroyplatform"); platformWidths = new float[Platformbegin.Length]; for (int i = 0; i < Platformbegin.Length; i++) { platformWidths[i] = Platformbegin[i].GetComponent().size.x; } }
void FixedUpdate()
{
if (transform.position.x < generationpoint.position.x)
{
distancebetween = Random.Range(distancemin, distancemax);
platformselector = Random.Range(0, Platformbegin.Length);
transform.position = new Vector3(transform.position.x + platformWidths[platformselector] + distancebetween, transform.position.y, transform.position.z);
cloneplatform = Instantiate(Platformbegin[platformselector], transform.position, transform.rotation);
}
if (cloneplatform.transform.position.x < Destroyt.transform.position.x)
{
Destroy(cloneplatform);
}
}
}
Comment