setActive(true) won't activate my object! Help Please.

Ok, what the hell is going on here. I don’t know if I’m just burned out from my real job but I’ve spent a long time trying to debug this thing.

TL;DR - I have a 2D list for object pooling which returns an inactive object and I try to setActive(true) but it does not work.

Detailed Background: I have a dynamic object pooler that will expand as needed. It is made using a 2D List (List of a List). As far as I can tell, everything is working correctly except for the setActive(true) part when I try to use my object pooler. In the editor, it shows my prefabs in the hierarchy and they are not parented or childed to anything. They’re just regular capsule prefabs. In my test scenario, I’m trying to create a pool of these capsules. I mentioned that it’s a dynamic object pooler, this means that I don’t currently have an “initial” pool of these capsules, this is all just happening at runtime. I call my Spawn() method, which calls GetObjectFromPool() which looks for an existing pool of the prefabs or calls AddToObjectPool to create the pool and then GetObjectFromPool recursively calls itself to return a prefab that AddToObjectPool just created. Ok, so that all works seemingly well. Spawn() keeps getting the first prefab from the pool because they’re always inactive and I can’t turn it on so it goes back to the pool.

Here’s my Object Pooler:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ObjectPooler : MonoBehaviour {

	//2D List of List of GameObjects.  For each Prefab, there's a pool of prefabs.
	List < List <GameObject> > pool = new List<List<GameObject>>();

	public GameObject GetObjectFromPool(GameObject ObjPrefab)
	{
		int ObjPrefabIndex = 0;

		for (ObjPrefabIndex = 0; ObjPrefabIndex < pool.Count; ObjPrefabIndex++) //Find the Object Type in the pool
		{
			if (pool[ObjPrefabIndex][0].name == ObjPrefab.name)
			{
				for (int i = 0; i < pool[ObjPrefabIndex].Count; i++)	//Find an inactive object to use
				{
					if (!(pool[ObjPrefabIndex]*.activeInHierarchy))*
  •  			{*
    
  •  				Debug.Log("Found object at: " + ObjPrefabIndex + " - " + i);*
    

_ return pool[ObjPrefabIndex];_
* }*
* }*
* break; // We have a pool of this prefab but there are no available objects. Get out of the loop.*
* }*
* }*

* //Since we made it this far, it means either the Object Prefab was not found or there were no available objects*
* //Create a pool of 5 objects and go through this method again*
* AddToObjectPool (ObjPrefab, 5);*
* return GetObjectFromPool (ObjPrefab); //Recursive call*

* }*

* public void ReturnObjectToPool(GameObject objectType)*
* {*
* objectType.SetActive(false);*
* }*

* public void AddToObjectPool(GameObject ObjPrefab, int numObjects)*
* {*
* int objPrefabIndex = 0;*

* Debug.Log("count: " + pool.Count);*

* // The outer loop is to go through the list of object prefabs.*
* // The inner loop is to actually create a pool of prefabs*
* for (objPrefabIndex = 0; objPrefabIndex < pool.Count; objPrefabIndex++)*
* {*
* if(pool[objPrefabIndex][0].name == ObjPrefab.name) //Object type found in the pool, create more copies of it*
* {*
* for(int i = 0; i < numObjects; i++)*
* {*
* //instantiate an object in the pool at the index of the desired object prefab*
* GameObject obj = (GameObject)Instantiate(ObjPrefab);*
* obj.name = ObjPrefab.name; //remove “(clone)” from the name*
* obj.SetActive(false);*
* pool[objPrefabIndex].Add(obj);*
* }*
* return;*
* }*
* }*

* // Since we got this far, it means we don’t have a pool for this prefab*
* // Create a new list entry for this prefab and create a pool for it.*

* List tempObjList = new List();*
* tempObjList.Add(ObjPrefab);*
* pool.Add (tempObjList);*

* for (int j = 0; j < numObjects; j++)*
* {*
* //instantiate an object in the pool at the index of the desired object type*
* GameObject poolObj = (GameObject)Instantiate(ObjPrefab);*
* poolObj.name = ObjPrefab.name; //remove “(clone)” from the name*
* poolObj.gameObject.SetActive(false);*
* pool[pool.Count - 1].Add(poolObj);*
* }*
* }*
And here’s my Spawn() method:
* public void Spawn(GameObject objectName, Vector3 spawnPosition)*
* {*
* GameObject obj = ObjPool.GetObjectFromPool(objectName);*
* obj.SetActive(true);*
* obj.transform.position = spawnPosition;*
* }*

If you don’t need to access anything in your script other than the GameObject functions, then I have found the best way to go about doing this is by declaring the item as a GameObject.

public GameObject yourObject;

void Function(){
      youObject.SetActive(true);
}

If you need to use something else on it then it would be best to do it using the .gameObject modifier in your code.

public Button yourButton;
    
void ActivateButton(){
     yourButton.gameObject.SetActive(true);
}

And to set the object to inactive replace true with false.