Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

GameObject.FindGameObjectsWithTag is Finding Objects On Preview Scene

Discussion in 'Scripting' started by 1ht1baron, Mar 5, 2018.

  1. 1ht1baron

    1ht1baron

    Joined:
    Aug 15, 2017
    Posts:
    65
    Hello everyone, i prepared a wave controller script. When GameObject.FindGameObjectsWithTag.Length.("Enemy") <= 0, wave works. But, there is a problem. Although scene has only 6 enemies, GameObject.FindGameObjectsWithTag.Length.("Enemy") is equal to 16. I killed 6 enemies and there is no enemies in scene but GameObject.FindGameObjectsWithTag.Length.("Enemy") is equal to 10 enemies. 10 enemies are in previes scene. I want GameObject.FindGameObjectsWithTag find tags on only scene. Not preview scene. How can i do?
     
  2. mehdimoji

    mehdimoji

    Joined:
    Oct 18, 2017
    Posts:
    22
    Why i don't have a .Length method in GameObject.FindGameObjectsWithTag ?o_O
    For the better answer, you need to put the code.But if you want to calculate the enemies you killed, you can create a variable : when kill ( Destroy ) EnemyKilled variable ++ and when you start a new scene or new level you can set it to zero in the Start function.
     
  3. 1ht1baron

    1ht1baron

    Joined:
    Aug 15, 2017
    Posts:
    65
    For example; When GameObject.FindGameObjectsWithTag ("Enemy").Length <= 2, next wave is coming. But although scene has only 2 enemies, GameObject.FindGameObjectsWithTag.Length.("Enemy") is equal to 12. So, next wave is not coming. I restarted unity and this problem fixed. It no longer counts on preview scene. But i do not understand. Is this a bug?
    Code (CSharp):
    1.     if (GameObject.FindGameObjectsWithTag ("Enemy").Length <= 2) {
    2.                 StartCoroutine ( SpawnWave ( Waves[waveIndex] ) );
    3.             return;
    4.             }    
     
  4. mehdimoji

    mehdimoji

    Joined:
    Oct 18, 2017
    Posts:
    22
    Sometimes programs get weird bug like this :) !