SetActive doesn't work.

Hey I’m trying to do some object pooling and I’ve managed to test it before successfully, but now my new script doesn’t want to activate the pooled objects. It must be something small that I’ve missed but I have no idea what.

Here’s the pooler itself:

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

public class ObjectPooler : MonoBehaviour
{

    public static ObjectPooler SharedInstance;

    [System.Serializable]
    public class SpawnData
    {
        public GameObject obj;
        public int amount;
    }
    public SpawnData[] spawnData;

    public List<GameObject> roadPooler;
    public List<GameObject> plotPooler;
    public List<GameObject> housePooler;
    public int[] poolAmount;


    private void Awake()
    {
        SharedInstance = this;
    }

    private void OnEnable()
    {
        int[] poolAmount = new int[3];
        int lenght = spawnData.Length;


        for (int i = 0; i < lenght; i++)
        {

            int amount = spawnData[i].amount;
            GameObject obj = spawnData[i].obj;

            for (int j = 0; j < amount; j++)
            {

                GameObject tmp = Instantiate(obj);
                tmp.SetActive(false);
                housePooler.Add(obj);
            }
        }

        poolAmount[(int)poolObject.house] = housePooler.Count;
    }

    // Start is called before the first frame update
    void Start()
    {



    }

    // Update is called once per frame
    void Update()
    {
       
    }


    public GameObject GetPooledHouse() //pool of buildings
    {
        int lenght = housePooler.Count ;
        if (lenght > 0)
        {
            int index = Random.Range(0, lenght);
            GameObject temp = housePooler[index];
            housePooler.Remove(temp);
            Debug.Log("Here");
            return temp;
        }

        return null; // if nothing in list return nothing



    }

}

and here’s he script trying to activate the objects:

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

public class BlockSpawner : MonoBehaviour
{

    float rotation;
    public GameObject[] Buildings;
    public int[] BuildingCount;
    bool test;
    // Start is called before the first frame update
    void Start()
    {


        if (transform.localPosition.z < 0)
        {
            rotation = 0;
        }
        else
        {
            rotation = 180;
        }

        transform.eulerAngles = new Vector3(0, rotation, 0);


    }

    private void OnEnable()
    {
            //int buildingNumber = Buildings.Length;
            //int random = Random.Range(0,buildingNumber);
            //Instantiate(Buildings[random],transform);
            GameObject _inst = ObjectPooler.SharedInstance.GetPooledHouse(); // spawn a new environment
            Debug.Log(_inst);
            _inst.SetActive(true);
            Debug.Log(_inst.activeInHierarchy);
            _inst.transform.position = transform.position;
            _inst.transform.eulerAngles = transform.rotation.eulerAngles;


    }


    // Update is called once per frame
    void Update()
    {



    }
}

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

Thanks for the reply. I already got a log for testing if the script gives me an instance when I call it with GetPooledHouse() and it indeed does give me one. So I’m wondering if there are some known occurances when SetActive doesn’t trigger?
II can try to draw a chart how I think the order goes if needed.

First I’d say check to see if your position and rotation are working, if those are working, then you need to discover if something is turning the object off. SetActive is not likely to break.

Next, I’d suggest you modify your code a bit as your pool can return null, but you don’t have a null check before you try to do anything with the object. The first time you hit null you’ll get a null ref error when you try to do anything with it. Note that this can also affect your code where it will just stop, which depending on how things are executed, could make it all look broken.

So, to answer your question, it’s not likely SetActive is bugged, so @Kurt-Dekker suggestion on how to fix this is helpful.

The other consideration is, do you even need a pooler or are you just doing it for fun? I’ve never bothered with them personally, and I do a lot of mobile stuff too.

Here’s my standard warning about optimizing for fun:

DO NOT OPTIMIZE CODE JUST BECAUSE… If you don’t have a problem, DO NOT OPTIMIZE!

If you DO have a problem, always start by using the profiler:

Window → Analysis → Profiler

Failure to use the profiler means you’re just guessing, making a mess of your code for no good reason.

Notes on optimizing UnityEngine.UI setups:

In this loop, you are never using j. So your loop just does the same thing “amount” times, is this your intention?

for (int j = 0; j < amount; j++)

Thanks for all the insights, although in the end I didn’t exactly find it by Logging. There was a slight error in the code which I had missed:

GameObject obj = spawnData[i].obj;


            for (int j = 0; j < amount; j++)
            {

                GameObject tmp = Instantiate(obj);
              tmp.SetActive(false);
              housePooler.Add(obj);
            }

I accidentally added the “prefab mold” into the List and not the Instantiated object.

Good point, altough it’s doing what it’s intended to do. But perhaps a repeat function would be the right way to do it. But can’t remember the syntax for one.

[quote=Kurt-Dekker]
The other consideration is, do you even need a pooler or are you just doing it for fun? I’ve never bothered with them personally, and I do a lot of mobile stuff too.

Here’s my standard warning about optimizing for fun:

DO NOT OPTIMIZE CODE JUST BECAUSE… If you don’t have a problem, DO NOT OPTIMIZE!

If you DO have a problem, always start by using the profiler:

Window → Analysis → Profiler

Failure to use the profiler means you’re just guessing, making a mess of your code for no good reason.
[/quote]

Yeah. You’re right with this one. I’m hellbent on optimizing even though I’m not even an experienced coder, I gotta let go of that attitude somehow. The profiler is indeed a nice tip! Thanks.

Oh man, computers (even mobile devices) are an absolute BEAST today, it’s amazing what you can get away with.

I would suggesting focusing more on finding ways to improve your ability to make fun and varied games… at least that’s where I find my reward from. Expose yourself to lots of different game tutorials, even stuff you don’t necessarily want to play, and you’ll learn a lot.

Remember that every single piece of optimization you do makes your code harder to work with, and that’s the last thing you want in your way, especially when learning!!