C# New to scripting, can anyone help get my object pool running smooth?

New to all this and not sure what im missing. Trying to spawn/pool multiple objects. I appreciate any help, like i said im a bit new. so still learning a few things, so dont be too harsh on me.

{

       
    private static PoolManager _instance;
    public static PoolManager Instance
    {
        get
        {
            if (_instance == null)
            {
                GameObject go = new GameObject("PoolManager");
                go.AddComponent<PoolManager>();
            }
            return _instance;
        }
   
    }

    public GameObject obstPrefab;

    public int obstaclesToSpawn = 20;
    public List<GameObject> obstacleList = new List<GameObject>();

    void Awake()
    {
        _instance = this;

    }

    void Start()
    {
        for(int i = 0; i < obstaclesToSpawn; i++)
        {
            GameObject obstacle = Instantiate(obstPrefab, Vector3.zero, Quaternion.identity) as GameObject;
            obstacle.transform.parent = this.transform;
   
            obstacleList.Add(obstacle);

        }
    }

}

Looks like your next step is to add the pool equivalent of Instantiate. It’ll have to loop through your obstacles, find one that’s inactive, activate it, and return it. I generally have this function mimic Instantiate, so you’ll feed in a position and rotation, and it’ll return the GameObject.

It might look something like this (untested)

public GameObject ActivateNew(Vector3 newPosition, Vector3 newRotation) {
foreach (GameObject go in obstacleList) {
if (!go.activeSelf) {
go.SetActive(true);
go.transform.position = newPosition;
go.transform.rotation = newRotation;
return go;
}
}
return null;
}
//in other scripts
GameObject newThing = PoolManager.instance.ActivateNew(Vector3.zero, Quaternion.identity);

Here are two implementations of the object pool that you can steal as needed.

This one by Unity

This one by me

They work on slightly different principles, with advantages and disadvantages to each. But you should be able to read through them and get a feel for how pooling works.

Edit: This question on answers is good too.