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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

CS1061 error for 'GameObject'[] does not contain a definition for 'transform'

Discussion in 'Scripting' started by mazzeku, Sep 13, 2022.

  1. mazzeku

    mazzeku

    Joined:
    Aug 17, 2022
    Posts:
    2
    Getting this code, tried to find a solution on many other forums and tried to rewrite the code and check every spelling error, and nothing could be found...
    Severity Code Description Project File Line Suppression State
    Error CS1061 'GameObject[]' does not contain a definition for 'transform' and no accessible extension method 'transform' accepting a first argument of type 'GameObject[]' could be found (are you missing a using directive or an assembly reference?) Assembly-CSharp C:\Users\Mat\Game_1_01\Assets\Scripts\WaveSpawner.cs 33 Active
    It's showing me an error by highlighting .transform on line 20.
    Thanks in advance for your help.

    Code (csharp):
    1.  
    2. public class WaveSpawner : MonoBehaviour
    3. {
    4.     [System.Serializable]
    5.     public class Wave
    6.     {    
    7.         public Enemy[] ennemies;
    8.         public int count;
    9.         public float timeBetweenSpawns;
    10.     }
    11.     public Wave[] waves;
    12.     public Transform[] spawnPoints;
    13.     public float timeBetweenWaves;
    14.     private Wave currentWave;
    15.     private int currentWaveindex;
    16.     private Transform player;
    17.  
    18.     private void Start()
    19.     {
    20.         player = GameObject.FindGameObjectsWithTag("Player").transform;
    21.             StartCoroutine(StartNextWave(currentWaveindex));
    22.     }
    23.  
    24.     IEnumerator StartNextWave(int index)
    25.     {
    26.     yield return new WaitForSeconds(timeBetweenWaves);
    27.         StartCoroutine(SpawnWave(index));
    28.     }
    29.  
    30.     IEnumerator SpawnWave (int index)
    31.     {
    32.         currentWave = waves[index];
    33.  
    34.         for (int i = 0; i < currentWave.count; i++)
    35.         {
    36.             if (player == null)
    37.             {
    38.                 yield break;
    39.             }
    40.             Enemy randomEnemy = currentWave.ennemies[Random.Range(0, currentWave.ennemies.Length)];
    41.             Transform randomSpot = spawnPoints[Random.Range (0, spawnPoints.Length)];
    42.             Instantiate(randomEnemy, randomSpot.position, randomSpot.rotation);
    43.  
    44.             yield return new WaitForSeconds(currentWave.timeBetweenSpawns);
    45.         }
    46.     }
    47. }
    48.  
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    There are two different methods.
    FindGameObjectsWithTag
    and
    FindGameObjectWithTag
    the second which doesn't have a documentation page, but returns a drastically different result.
     
    mazzeku likes this.
  3. mazzeku

    mazzeku

    Joined:
    Aug 17, 2022
    Posts:
    2
    Thanks alot Panda, didnt notice this even after multiple reads. New to coding and didnt even knew there was ObjectsWithTag, just the autofill that lead me to this.
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    there is FindWithTag
    FindGameObjectWithTag was probably made obsolete, probably because the slight difference in its name causes confusion.
     
    mazzeku likes this.