Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class ObjectPool : MonoBehaviour
- {
- public static ObjectPool instance;
- private List<GameObject> poolObject = new List<GameObject>();
- public int AmountPooled = 10;
- [SerializeField] private GameObject Objprefab; // Whatever object you want to pool.
- /// <summary>
- /// Awake is called when the script instance is being loaded.
- /// </summary>
- private void Awake()
- {
- if (instance == null) {
- instance=this;
- }
- }
- /// <summary>
- /// Start is called on the frame when a script is enabled just before
- /// any of the Update methods is called the first time.
- /// </summary>
- private void Start()
- {
- for (int i = 0; i < AmountPooled; i++)
- {
- GameObject obj = Instantiate(Objprefab);
- obj.SetActive(false);
- poolObject.Add(obj);
- }
- }
- public GameObject GetPooledOjbect() {
- for (int i = 0; i < poolObject.Count; i++)
- {
- if (!poolObject[i].activeInHierarchy) // if not active in hierarchy..
- {
- return poolObject[i];
- }
- }
- return null; // else return null.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement