Load object based on another object pool party?

I have the error code "NullReferenceException: Object reference not set to an instance of an object. I have loaded a pool with different prefabs and then activated them based on an int (the int references the prefab that must be activated) with this code:

public static firstPoolPartyScript SharedInstance;
public List<GameObject> pooledObjects;
public List<ObjectPoolItem> itemsToPool;
[System.Serializable]
public class ObjectPoolItem{
public int amountToPool;
public GameObject objectToPool;
public bool shouldExpand;
}

void Awake()
{
    SharedInstance = this;
}

void Start()
{
   pooledObjects = new List<GameObject>();
   foreach (ObjectPoolItem item in itemsToPool) {
   for (int i = 0; i < item.amountToPool; i++) {
   GameObject obj = (GameObject)Instantiate(item.objectToPool);
   obj.SetActive(false);
   pooledObjects.Add(obj);
   }
   }
}

public GameObject GetPooledObject(int tag) {
    for (int i = 0; i < pooledObjects.Count; i++) {
    if (!pooledObjects[i].activeInHierarchy && pooledObjects[i].GetComponent<placePrefab>().prefabType == tag) {
    return pooledObjects[i];;
    }
    }
    foreach (ObjectPoolItem item in itemsToPool) {
    if (item.objectToPool.GetComponent<placePrefab>().prefabType== tag) {
    if (item.shouldExpand) {
        GameObject obj = (GameObject)Instantiate(item.objectToPool);
        obj.SetActive(false);
        pooledObjects.Add(obj);
  return obj;
    }
    }
    }
  return null;
    }


void CreateObject()
{
GameObject lastObject = firstPoolPartyScript.SharedInstance.GetPooledObject(prefabType);
}

Now on a seperate code I have the same code to instantiate a new pool party of different prefabs and I want to instantiate these new prefabs based on the last prefabs that are activated so the code is the same as the last but with these changed lines:

public GameObject GetPooledObject(int tag) {
    for (int i = 0; i < pooledObjects.Count; i++) {
    if (!pooledObjects[i].activeInHierarchy && firstPoolPartyScript.pooledObjects[i].GetComponent<placePrefab>().prefabType2 == tag) {
    return pooledObjects[i];
    }
    }
    foreach (ObjectPoolItem item in itemsToPool) {
    if (item.objectToPool.GetComponent<placePrefab>().prefabType2 == tag) {
    if (item.shouldExpand) {
    GameObject obj = (GameObject)Instantiate(item.objectToPool);
    obj.SetActive(false);
    pooledObjects.Add(obj);
  return obj;
    }
    }
    }
  return null;
    }

The error is here in this line:

if (!pooledObjects[i].activeInHierarchy && firstPoolPartyScript.pooledObjects[i].GetComponent<placePrefab>().prefabType2 == tag) {

I am guessing the error is because it can’t read pooledObjects in reference to prefabType

The answer is always the same… ALWAYS!

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

A word of warning on pooling:

The costs and issues associated with object pooling / pools:

Well what code can be used to clean up instantiated objects? Is there any code for that because my memory builds up and causes a memory leak

Destroy() is used to clean up instantiated objects.

Okay so the memory leak was an issue in Unity 2022.2.12f1 and upgrading to 2022.2.16f1 seemed to fix it. I still want to do this object pooling though

Edit: Oh no it didn’t fix it after all. Meshes in the memory profiler shows that my meshes grow every second and takes up all the memory until a memory leak occurs. Even if I delete all the assets in the assets folder and create a new scene

I’m not sure where the memory leak conclusion cane from but let’s start from the null pointer issue:

  • Have you attached your IDE to Unity and put a breakpoint in that line, to inspect the variables in that line and see which ones are null?

  • What do you mean with: in reference to prefabType? The placePrefab type, the prefabType2 field or something else?

  • If there is a null pointer in that line, it could be because:

  • The list in pooledObjects has not yet been initialized because Start hasn’t been called yet. That could e.g. be because you’re calling GetPooledObject() from another Start method that executes earlier

  • pooledObjects has been Destroy()ed, either via Destroy or via a scene unload.

    • Is this second pooling script inheriting from the first? If so, is it setting the firstPoolPartyScript of the first class? Why are there presumably 3 different static fields to these pools (also Shared instance and firstPoolPartyScriptSharedInstance)? That sounds like a receipt for pain. If these are supposed to be singletons, it’s a good safety precaution to check that the SharedInstance is null in awake (or else throwor log an error) as well as to set the static fields to null in On destroy so that these references don’t survive a level unload and leak the managed shell of this script. That said, this*
  • firstPoolPartyScript could be null depending on how the rest of the code for that is handling it. Also, why are you first checking the pooledObjects list on this instance and then continue on the statically referenced instance? Are those supposed to be the same? If so, stick with one or the other, don’t mix and match or chaos might ensue.*

    • If the static instance is not the same as the current, 1-3 could apply to it*
    • The gameobject might not have the placePrefab component on it, though that might throw a different error.*
  • [/i]*

  1. You have just introduced me to breakpoints
  2. I am activating objects of a pool based on their int then I want to activate more objects from another pool based on the same int
  3. I don’t think anything is null except for if (!pooledObjects[i].activeInHierarchy && firstPoolPartyScript.pooledObjects[i].GetComponent<placePrefab>().prefabType2 == tag) { because I want “i” to point at the same “i” as the first pool party

That line makes no sense to me, possibly because there is context missing.
firstPoolPartyScript.pooledObjects
Looks like you’re accessing a static poolObjects list on the type
firstPoolPartyScript, yet in the first snippet, that variable isn’t static, so that wouldn’t even compile. So I guess that this is a field of this type that is feed in from
firstPoolPartyScript.SharedInstance at some point? Are you sure it isn’t null?

When you hit this line via a breakpoint and hover each segment of the line with your mouse to see what value each variable holds, which one is null exactly?

Also, you’re checking activeInHierarchy on one list and then on a completely different one, you use the same index (not tag, but the position in a list as index ‘i’, that could be completely different in length and could be out of range of the for loop’s guard rails for ‘i’) to check the placePrefab component.
There could be context missing to clarify how that could be save, and would not lead to null pointers, IndexOutOfBounds Exceptions or general unexpected behavior. But it certainly doesn’t read well and if it raises that many questions while reading, should probably be changed to be more self explanatory.