Search Unity

'Script' does not contain a defitition for 'Length'

Discussion in 'Scripting' started by SamuraiKenshinC, Mar 27, 2020.

  1. SamuraiKenshinC

    SamuraiKenshinC

    Joined:
    Feb 10, 2020
    Posts:
    19
    So while trying to script something that could check how many prefabed game objects have been destroyed from the spawner script, I came across an error saying I wouldn't convert a class to an integer. This shouldn't have been a problem because the tutorial where I got this encountered the same error and fixed it with putting .Length at the end of the line. So I do this and I get an error saying that the class that the line of code is referencing doesn't have a definition for Length. So then I played around with it and I think the line thinks that I'm trying to call on a variable from a different class. Any help would nice (also if you couldn't tell, I'm pretty new to all of this)

    TLDR: .Length is being a butt plz help (Line 27)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Spawner : MonoBehaviour
    6. {
    7.    
    8.     public GameObject wallPrefab;
    9.     private int randWall;
    10.     private float spawnWait;
    11.  
    12.     public float spawnFreq = 10;
    13.  
    14.     public int wallCount;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.  
    20.  
    21.         InvokeRepeating("SpawnWall", 0, spawnFreq);
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         wallCount = FindObjectOfType<Wall>().Length;
    28.  
    29.         if (wallCount == 0)
    30.         {
    31.             spawnFreq = spawnFreq - 0.5f;
    32.         }
    33.        
    34.     }
    35.  
    36.     void SpawnWall()
    37.     {
    38.         Vector3 spawnPos = new Vector3(Random.Range(-10, 10), 1.5f, 0);
    39.        
    40.  
    41.         Instantiate(wallPrefab, spawnPos, wallPrefab.transform.rotation);
    42.     }
    43.  
    44.  
    45. }
    46.  
     
  2. willemsenzo

    willemsenzo

    Joined:
    Nov 15, 2012
    Posts:
    585
    You probably want to use FindObjectsOfType because that returns an array, but the function you are calling is returning a single object.