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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

array.length not working when trying to instantiate multiple prefabs via an array

Discussion in 'Scripting' started by AndrewMac26, Dec 10, 2015.

  1. AndrewMac26

    AndrewMac26

    Joined:
    Nov 24, 2015
    Posts:
    14
    So im trying to spawn in three instances of targetPrefab using an array but I get an error that say spawnScript doesn't contain a definition for 'length' . any idea why this is ? Here's the relevant code:
    Code (CSharp):
    1. public class multipleTargetSpawn : MonoBehaviour {
    2.  
    3.     public spawnScript[] spawnScripts;
    4.  
    5.  
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.  
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update () {
    14.  
    15.     }
    16.  
    17.     void OnTriggerEnter(Collider other)
    18.     {
    19.         if (other.gameObject.tag == "player")
    20.         {
    21.             for (int i = 0; i < spawnScript.Length ; i++)
    22.             {
    23.                 spawnScripts[i].Spawn();
    24.             }
    25.         }
    26.     }
    27. }
    28.  
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    You wrote spawnScript (the type) instead of spawnScripts (the array)
     
  3. RavenMikal

    RavenMikal

    Joined:
    Oct 18, 2014
    Posts:
    144
    Normally to avoid that I set the length before actually setting the value. So for that, I would have added:
    Code (csharp):
    1. spawnscript = new spawnScripts[2];
    And he's right...lol. Gotta love syntax >_<
     
  4. AndrewMac26

    AndrewMac26

    Joined:
    Nov 24, 2015
    Posts:
    14
    Yep thats its thanks guys, just needed some fresh eyes to look at it I suppose
     
  5. RavenMikal

    RavenMikal

    Joined:
    Oct 18, 2014
    Posts:
    144
    Know the feeling >_<