Active gameobjects issue

Updated on April 24, 2016 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on April 24, 2016

Hi everyone,

I have an issue with gameobjects in the scene. So, first of all I would like to understand when a gameobject is active: from what I understood, it is active even if it’s just inside my prefab folder. So even if my prefab is not physically inside the scene (cannot be seen in-game), apparently it’s still active, correct?

So, assuming I understood the concept correctly, my question now is: how can I ONLY look at gameobjects which are physically inside my scene? I am currenlty using “FindGameObjectsWithTag(“Enemies”)”, but some enemy prefabs are not physically in the scene yet, they will be spawned later in the game. Does this command also take into accountthose “inactive” prefabs?

Thanks in advance!

  • Liked by
1 on April 24, 2016

Using FindGameObjectsWithTag(“Enemies”) will only return GameObject’s currently in your scene, so if you need to update the array/list that you are adding these enemies to – be sure to call this method once more whenever you spawn another enemy.

Otherwise some of your enemies won’t end up in the array/list, which I assume would cause problems for you.

on April 24, 2016

Ok, I think that’s the issue. I use

foreach (GameObject bot in allBots){…}

where allBots is the array which should theoretically contain all enemies, and this loop is put inside a void function. But apparently is not getting all my enemies because they are spawning at different moments in time..

Thanks I will try looking into that.

Show more replies
  • Liked by