Moving an Inactive gameobject after period of time

Hello, I’ve started trying to replicate the game cuphead, I’m trying to make a boss level like the 3 Vegetables level. At the moment I have 3 phases, where in each phase the boss does a different type of attack. Now I want to have the boss move to a new position when its time for the new phase. In the cuphead level, how I imagined it happening is: Damage boss, Move on to next stage, boss turns inactive (due to it being invisible and colliders disappearing etc), then after amount of time, boss is active again. So I wrote my code like such :

` IEnumerator Move(Vector3 position, float time = 0f)
{
gameObject.SetActive(false);

    if (transform.position != position)
    {
        transform.position = position;
    }

    yield return new WaitForSeconds(time);

    gameObject.SetActive(true);
}`

And it gets called in the void Update() function like so: if (healthPercent <= 1f) { if (!attack1Started) { attack1Started = true; StartCoroutine(Move(positions[0], 0)); StartCoroutine(Attack1()); } }

However I get an error saying “Coroutine couldnt be started because the gameobject is inactive”.
What would be the correct way of doing this? Would it be better to remove the sprite and disable the colliders so its similarly invisible? Or better way of scripting? For reference here is the boss fight I’m taking about Cuphead: The Root Pack Boss Fight #1 - YouTube

GameObjects that are inactive can be referenced just fine, and any inactive Component, or Component on an inactive GameObject can have functions run on them without any problems. Being made inactive only does a few things: fire OnDisable on every disabled script, stop all coroutines and invokes that are currently running on those scripts (and prevent you from starting more, as you’ve now seen), the update cycle functions on each script will stop firing (so like Update/LateUpdate/FixedUpdate will all stop), and scene-searching functions like GameObject.Find will no longer be able to locate that object by name. Specific Components also have their own internal OnDisable and OnEnable-like functions that you can’t see, so Colliders and Rigidbodies will remove themselves from the Physics system temporarily, MeshRenderers will stop rendering the mesh, Animators will stop animating the avatar, etc, but they can still be accessed and data pulled from them and set to them just fine.

Being disabled definitely doesn’t make any existing references to the GameObject or Components attached to it null- only actually destroying the GameObjects or Components will do that, so anything that previously obtained a reference will still hold that reference, and you can still use GetComponent / GetComponentInParent / GetComponentInChildren, etc, just fine (for the Children versions though, you have to set the optional second parameter “includeInactive” to true). As an added note: all disabled Components will also still receive collision and trigger events, if the GameObject they’re attached to is active and has an active collider/trigger, so that they can re-enable themselves if needed.

As to your specific problem, I would make a manager that controls the state of the boss (and other enemies, probably). It can be a parent of the boss, or it can be a completely different GameObject, or it can just be a static manager and not a GameObject at all, but either way it should be the one that holds the boss’s state, and it should be the one that re-enables it after X seconds. It depends on whether you only want the boss to be invisible, but still acting, or whether it’s not going to be doing anything at all for those seconds. One option might be to have one boss object that’s the manager, and multiple children with logic that function as separate states of that boss (one or more may have the big visual sprite representing it). This makes it so you can do things like have objects being thrown in from the sides of the level during some boss stages, or dropping from the sky, etc, but disable that set of behaviours as needed, while enabling others…

In the end, it’s really up to you- there are dozens of ways of handling this, and each has their advantages and disadvantages. Whatever seems easiest / most logical for what you need should be your approach.

Good day.

When a Gameobject like “Apple” is SetActive(false), all scripts trying to interact with Apple scripts or transforms or components, will get null.

If you will need that object in the future, but need to “disappear” fora time, first, all scripts that will need to interact with apple, needs to find it and store the GameObject before apple gets SetActive(false). this way some things can be changed while Apple is SetActive(false), but depending on what you need to change from apple while is SetActive(false) you will need to wait to SetActive(true) tod o it.

The other option you have is or enable=false all components of the apple like colliders and images, or maybe just move the apple outside the camera view while you need.

Good luck !