Void Avake() and SetActive

Hi all,

I have a spawner gameObject that instantiates an enemy ship. When the enemy ship gets destroyed, the spawner gameObject gets deactivated with SetActive(). After a while, when some decided action happens, the same spawner gets activated again. I want that spawner to instantiate the same gameObject again.

So my question is if I call the method that instantiates the ship in Awake(), would the enemy ship instantiate?
That’s what I understand from the documentation but I couldn’t manage to make that happen. The spawner gets enabled and spawns the ship, the ship gets destroyed and disables the spawner, after some designated action the spawner gets enabled again but does not instantiate the ship again.

This on the spawner Manager,

 void NormalSpawnerDeactivator()
    {
        if (enemyDeathCount1 >= 20 && enemyDeathCount1 <= 22)
        {
            normalSpawnerIndex = 1;
            if (player.transform.position.x < anchBottom.transform.position.x && instantiated==false)
            {
                normalSpawner1.SetActive(false);
                questSpawner2.SetActive(true);
                instantiated = true;
                

            }
            else if (player.transform.position.x > anchBottom.transform.position.x && instantiated == false)
            {
                normalSpawner1.SetActive(false);
                questSpawner1.SetActive(true);
                instantiated = true;
            }
        }
        else if (enemyDeathCount1 >= 50 && enemyDeathCount1 <= 54)
        {
            
            normalSpawnerIndex = 2;
            if (player.transform.position.x < anchBottom.transform.position.x && instantiated == false)
            {
                normalSpawner2.SetActive(false);
                questSpawner2.SetActive(true);
                instantiated = true;

            }
            else if (player.transform.position.x > anchBottom.transform.position.x && instantiated == false)
            {
               
                normalSpawner2.SetActive(false);
                questSpawner1.SetActive(true);
                instantiated = true;
            }
        }
    }
void QuestSpawnerDeactivator()
    {
        if (isDestoryed && normalSpawnerIndex==1)
        {
            questCoundown -= Time.deltaTime;
            if (questCoundown <= 0f)
            {
                questSpawner1.SetActive(false);
                questSpawner2.SetActive(false);
                normalSpawner2.SetActive(true);
                questCoundown = 0;
                instantiated = false;
                isDestoryed = false;
            }
          
        }
        else if (isDestoryed && normalSpawnerIndex == 2)
        {
            questCoundown -= Time.deltaTime;
            if (questCoundown <= 0f)
            {
                questSpawner1.SetActive(false);
                questSpawner2.SetActive(false);
                normalSpawner3.SetActive(true);
                questCoundown = 0;
                instantiated = false;
                isDestoryed = false;
            }
        }
    }

and this is on the spawner gameObject that I want to enable and disable which is a child of the spawner manager.

  private void Awake()
    {
        MiniBoss();
        
    }
   

    void MiniBoss()
    {
        GameObject mB = Instantiate(miniBoss, mBspawnPosition.position, Quaternion.identity) as GameObject;
        Debug.Log("Instantiate");
    }

The miniBoss gameObject gets destroyed through its own script and turns the bool isDestroyed = true with OnDestroy().
Thanx in advance…cheers=)

Hi,

Do you know the OnEnable method? Maybe that’s what you are looking for to make the spawner spawn the same enemy ship after it was enabled again.

Now that I read your post, I remember that I read about onEnable on different posts, etc., but I never used it before. onEnable worked perfectly and I will mark your post as the solution. But I wanted to ask if it is obvious why the Awake() method doesn’t work in this situation.

“Awake is called either when an active GameObject that contains the script is initialized when a Scene loads, or when a previously inactive GameObject is set to active…”

There is this sentence in the documentation for Awake(). Am I interpreting it wrong?
Thanks =)

Awake gets called only once during the lifetime of a game object. However, the quoted description is indeed a bit ambiguous. Awake only gets called if the “previously inactive GameObject” has never been active before. For this reason, I can see why you tried to make your idea work with Awake. :slight_smile:

Thnx Nina…I really appreciate the help…cheers…=)

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.