Another noob question!

Just trying to make my systems more idiot proof moving forward so if I forget to add something to a scene the game will still work. This works, but I thought it wouldn’t when I wrote it.

    public ObjectStack(T Object, int Size)
    {
        // The object Stack manager is required.
        if (ObjectStackManager.objectStackManager == null)
        {
            Debug.Log("GameManager needs an ObjectStackManager component!");
            GameManager.gameManager.gameObject.AddComponent<ObjectStackManager>();
        }

        pool = new T[Size + 1];
        Debug.Log(pool.Length);
        pool[0] = Object;
        size = Size;
        ObjectStackManager.objectStackManager.AddTask(FillStack(size));
    }

When I construct this object stack, it checks to see if a singleton manager script exists and if it doesn’t it creates it. The constructor immediately uses a method of this manager. I thought this would be a problem, since the manager would not exist until after this script executes the rest of its code.

Is my understanding correct that the AddComponent line branches off and does the construction/instantiation Awake method of ObjectStackManager before returning to this code block at line 10?

For your curiosity: currently the stack manager does two things: it defers the execution of a method that populates stacks with Instantiate calls so I can add stacks during gameplay. It also defers the destruction of objects in a stack over time so the garbage collector doesn’t try to clean up a whole stack at once, before destroying the actual stack object itself. It can do this for multiple stacks at once, and when it does it staggers them.

Yes. Awake will get called immediately after the component is added to the newly created game object, before moving forward to line 10.

Thanks, it made sense I just… Didn’t want to count on it without somebody saying so.