- Home /
Question by
jchan20 · Sep 15, 2018 at 04:28 PM ·
c#object poolinfinite runner
Iam making endless runner and i have array of gameobjects as obstacles and i can make loop but i need more efficient way to destroy or reuse gameobjects.
public class InfiniteObstacles : MonoBehaviour
{
public GameObject[] ObstaclePrefabs;
private float CurrentPos = 0;
private float AddPos = 11f;
private int RandomObstacle;
public Transform camera;
private void Start()
{
InvokeRepeating("SpawnObstacles",.5f,1);
}
private void Update()
{
}
void SpawnObstacles()
{
CurrentPos += AddPos;
int RandomObstacle = Random.RandomRange(0,ObstaclePrefabs.Length);
GameObject ob= Instantiate(ObstaclePrefabs[RandomObstacle]);
ob.transform.position = new Vector3(CurrentPos,0f,0f);
ob.transform.rotation = Quaternion.identity;
}
}//class
Comment