After updating the game according to the specifications given in the answer I now get the following error message:
Assets\ObjectPooler.cs(79,41): error CS0103: The name 'pool' does not exist in the current context. I understand why it doesn't work since it's declared in another method, but how can I change my code to access this variable?
Thanks in advance
I haven't added the release and .sceneloaded scripts to the game yet
This is my ObjectPooler script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectPooler : MonoBehaviour
{
[System.Serializable]
public class Pool
{
public string tag;
public GameObject prefab;
public int size;
}
#region Singleton
public static ObjectPooler Instance;
private void Awake()
{
// Already another instance?
if (Instance)
{
Destroy(this.gameObject);
return;
}
Instance = this;
DontDestroyOnLoad(this.gameObject);
}
#endregion
public List<Pool> pools;
public Dictionary<string, Queue<GameObject>> poolDictionary;
// Start is called before the first frame update
//change this method to make it work everytime level is loaded
private Dictionary<string, Pool> prefabPools;
private void Start()
{
foreach (var pool in pools)
{
Queue<GameObject> objectPool = new Queue<GameObject>();
for (int i = 0; i < pool.size; i++)
{
GameObject obj = Instantiate(pool.prefab);
obj.transform.SetParent(transform);
obj.SetActive(false);
objectPool.Enqueue(obj);
}
prefabPools.Add(pool.tag, pool);
poolDictionary.Add(pool.tag, objectPool);
}
}
public GameObject SpawnFromPool(string tag, Vector3 position, Quaternion rotation)
{
if (!poolDictionary.ContainsKey(tag))
{
Debug.LogWarning("Pool with tag" + tag + " doesn't exist.");
return null;
}
GameObject objectToSpawn;
// check if there are objects left otherwise insteantiate a new one
if (poolDictionary[tag].Count > 0)
{
objectToSpawn = poolDictionary[tag].Dequeue();
}
else
{
objectToSpawn = Instantiate(pool.prefab);
objectToSpawn.transform.SetParent(transform);
}
objectToSpawn.transform.position = position;
objectToSpawn.transform.rotation = rotation;
objectToSpawn.SetActive(true);
IPooledObject pooledObj = objectToSpawn.GetComponent<IPooledObject>();
if (pooledObj != null)
{
pooledObj.OnObjectSpawn();
}
return objectToSpawn;
}
}
This is the IPooledObject interface:
using UnityEngine;
public interface IPooledObject
{
void OnObjectSpawn();
}
This is how the gameObject in the pool gets called from the scripts:
objectPooler.SpawnFromPool("Ball", spawnPoints[randomSpawnPoint].position, Quaternion.identity);
The way it should work is that when I transition between the different scenes of my game the Object Pooler a new instance of the object pooler gets created or it gets reset and they appear on the screen and behave according to the scripts instead of not appearing and acting as if they were destroyed. Some things to note is that if one the objectpooler and objects behave normally in the first scene and only start throwing the error message when the game transitions between scenes and 2 when I edit the script to instantiate the object without using the objectpooler as in:
Instantiate(interact[Interact], spawnPoints[randomSpawnPoint].position,
Quaternion.identity);
Where I store the gameobjects prefabs in a public array of name interact and call them by index, the script also seems to work. However I need to be able to use an ObjectPooler inorder to prevent my code from becoming expensive?