TextUI pool won't reappear

Hi,
I have a pool of textUI prefab objects.

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

class HitCountPool
{

    public static int poolNum;
    public static Text pooledObj;
    public static List<Text> hitCountPool;

    // Use this for initialization
    public static void init(Text prefab, int initQuantity)
    {

        //only init if it hasn't init
        if (hitCountPool == null)
        {
            pooledObj = prefab;
            poolNum = initQuantity;

            hitCountPool = new List<Text>();

            //init pool
            for (int i = 0; i < poolNum; i++)
            {

                //Text obj = Text.Instantiate(pooledObj, new Vector3(-100,-100), Quaternion.identity) as Text;
                Text obj = Text.Instantiate(pooledObj) as Text;
                //looks like you have to put it in a canvas to have it show up
                obj.transform.SetParent(GameObject.Find("UICanvas").transform, false);
                obj.gameObject.SetActive(false);
                hitCountPool.Add(obj);

            }
        }
        //just set the gameobject to obj.SetActive(false) to send back to pool

    }



    public static Text GetObject()
    {
        
        //Debug.Log("Text count: " + hitCountPool.Count);
        for (int i = 0; i < hitCountPool.Count; i++)
        {
            Debug.Log("GOT Text pool: " + hitCountPool[i].gameObject.activeSelf);
                      
            if (!hitCountPool[i].gameObject.activeSelf)
            {
                Debug.Log("obj: " + hitCountPool[i].rectTransform.position);
                hitCountPool[i].gameObject.SetActive(true);

                return hitCountPool[i];
            }
        }

        //if need to grow
        Debug.Log("Text grow");
        Text obj = (Text)Text.Instantiate(pooledObj) as Text;
        obj.transform.SetParent(GameObject.Find("UICanvas").transform, false);
        hitCountPool.Add(obj);
        return obj;
        


    }

}

The objects shows up as deActivated objects as children of the Canvas in the hierarchy. It shows up when I call this function in my Enemy classes.

    public virtual void HitCounter()
    {
        hitCounter++;
        if (hitCounter >= hitCount)
        {
            Die();
        } else {
            //display hitcount
            //dust
            Text obj = HitCountPool.GetObject();
            if (obj == null)
                return;
       
            obj.gameObject.SetActive(true);
            if(obj.gameObject.activeSelf) {
                Debug.Log("obj is set ACTIVE");

            }
            //obj.enabled = true;

            Vector2 pos = Camera.main.WorldToScreenPoint(hero.transform.position);
            Debug.Log("hero POS: "+ pos);

            obj.rectTransform.position = pos;
            

            obj.GetComponent<HitCountController>().PlayAnimation("-"+ (hitCount - hitCounter+1));
           
        }

    }

The Text prefab has a controller script attached that moves the text and deactivates it.

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


public class HitCountController : MonoBehaviour {

    private bool init = false;
    private float step = 0.0f;
    private float rate = 1.8f;
    private Vector2 pointA;
    private Vector2 pointB;


    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        if(init) {
            step += (Time.deltaTime / rate);

            gameObject.transform.position = Vector2.Lerp(pointA, pointB, step);
         
            if (Vector2.Distance(transform.position, pointB) < .01f)
            {
                init = false;
                gameObject.transform.position = new Vector2(500,500);
                 gameObject.SetActive(false);
    

            }

        }

    }
    // object's Y is set outside of view for some reasonß
    public void PlayAnimation(string count) {
        init = true;
        pointA = transform.position;
        pointB = new Vector2(transform.position.x, transform.position.y + 600.0f);
        Debug.Log("textPOS: "+pointA+":"+GetComponent<Text>().rectTransform.position);
        GetComponent<Text>().text = count;




    }
}

That problem is once it deactivates (SetActive(false)), it won’t show up again. obj.gameObject.SetActive(true) in HitCounter() doesn’t seem to work; maybe I’m trying to access the wrong object?

It certainly SEEMS like it should work. To isolate your problem, why don’t you make a single canvas with a single text object, then put another button on it that calls .SetActive(false) and .SetActive(true) on the text object and verify that this works.

If that fails, then we have a misunderstanding of what Unity is supposed to do, or else a Unity UI bug (it happens).

ALSO, try a button function to clone a turned-off prefab, parent it, etc., and if THAT doesn’t appear, then you can concentrate on that flow. It’s probably just an order of enabling/disabling/cloning.

If that works, then there really is a logic problem in your pooling, and you can concentrate on that issue with a much smaller set of items, like maybe one in the pool, one being used, and a pared-down test scene to test it via button control.

Thanks. I found the problem. The textUI was killed (SetActive(false)) immediately upon reappearing. SO the animating function was the problem. I made a simple Coroutine to move the text arbitrarily instead.

It only took a day to figure this out. Time waister!

1 Like