Thank you for the quick reply and yes I am new to C# programing. This is my ObjectPool.cs
*
using System.Collections;
using System.Collections.Generic;
public class ObjectPool : MonoBehaviour {
public static ObjectPool instance;
public GameObject[] objectPrefabs;
public List<GameObject>[] pooledObjects;
public int[] amountToBuffer;
protected GameObject containerObject;
void Awake() {
instance = this;
}
void Start() {
containerObject = new GameObject("ObjectPool");
pooledObjects = new List<GameObject>[objectPrefabs.Length];
int i = 0;
for(int j = 0; j < objectPrefabs.Length; j++) {
pooledObjects *= new List<GameObject>();*
int bufferAmount = amountToBuffer*;*
for(int n = 0; n < bufferAmount; n++) {
GameObject newObj = Instantiate(objectPrefabs[j]) as GameObject;
newObj.name = objectPrefabs[j].name;
PoolObject(newObj);
}
i++;
}
* }*
* public GameObject GetObjectForType(string objectType, bool onlyPooled) {*
for(int i = 0; i < objectPrefabs.Length; i++) {
GameObject prefab = objectPrefabs*;*
if(prefab.name == objectType) {
if(pooledObjects*.Count > 0) {*
GameObject pooledObject = pooledObjects*[0];*
pooledObjects*.RemoveAt(0);*
pooledObject.transform.parent = null;
pooledObject.SetActive(true);
return pooledObject;
} else if(!onlyPooled) {
return Instantiate(objectPrefabs*) as GameObject;*
}
break;
}
}
return null;
}
public void PoolObject(GameObject obj) {
for(int i = 0; i < objectPrefabs.Length; i++) {
if(objectPrefabs*.name == obj.name) {*
obj.SetActive(false);
obj.transform.parent = containerObject.transform;
pooledObjects*.Add(obj);*
return;
}
}
}
}
*
And this is Obstacle.cs I assign to my object obstacle1 prefab.
using System.Collections;
public class Obstacle : MonoBehaviour {
public Vector3 velocity = new Vector3(-2.5f, 0);
public AudioClip sound;
private Transform cachedTransform;
private bool hasEnteredTrigger = false;
void Awake() {
cachedTransform = transform;
}
* void Update() {*
cachedTransform.Translate(velocity * Time.smoothDeltaTime);
if(!isVisible()) {
Deactivate();
}
* }*
void OnEnable() {
cachedTransform.position = new Vector3(11, Random.Range(-3.0f, 3.0f), 5);
}
void OnDisable() {
cachedTransform.position = new Vector3(-9999, 0, 5);
hasEnteredTrigger = false;
}
bool isVisible() {
bool result = true;
Vector2 screenPosition = Camera.main.WorldToScreenPoint(cachedTransform.position);
if(screenPosition.x < -100) {
result = false;
}
return result;
}
void Deactivate() {
ObjectPool.instance.PoolObject(gameObject);
}
void OnTriggerEnter2D(Collider2D other) {
if(hasEnteredTrigger == false && other != null && other.CompareTag(“Player”)) {
Score.TotalScore += 1;
AudioSource.PlayClipAtPoint(sound, new Vector3(0, 0, 0), 1.0f);
hasEnteredTrigger = true;
}
}
}
*
Thank you for your time and help…