Search Unity

Instantiating an Array of GameObjects on Start

Discussion in 'Scripting' started by Frostbite23, May 24, 2015.

  1. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    Hello, I'm creating a navigation app for a company and to make this app future proofing I want to instantiate stuff at runtime so when I have to update something I can just update the script. Right now I'm having trouble Instantiating an array of objects I have.

    Heres my code.
    Code (JavaScript):
    1. #pragma strict
    2.  
    3.  
    4. var position : float = 0.0f; //we will add this
    5. var scale : float = 0.0f; //set this as the scale
    6. var buttons : Sprite[];
    7.  
    8.  
    9.  
    10. function Start () {
    11.     var emptyObject : GameObject = new GameObject("DropDown");
    12.     emptyObject.transform.parent = transform;
    13.  
    14.     for(var i = 0; i < buttons.length; i++){
    15.       var buttons : Sprite = new Sprite(Instantiate(buttons[i], transform.position, transform.rotation));//i know this line has errors but you should be able to see what I'm tryina do
    16.     }
    17. }
    So what I have is a new game object being created into the scene so it will be containing my "soon to yet be created array of game objects that will be created in the scene and parented to at start".

    Ive searched every forum and post out there but non seem to fit or work for me. Can someone help me out please?

    Question = "How can I Instantiate an array of GameObjects?"
     
    Last edited: May 24, 2015
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,699
    I would use a List. With a list you can add, and remove objects. With an array, you can't.
     
  3. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    But heres the thing, I don't want to add or remove objects from my list or array. All I want is to put them in the scene, Instantiate them into the scene. Thats what I'm looking for. Thanks for the suggestion though
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you've declared an array, i.e. "dear compiler, this name is referring to an array of Sprite types", but you haven't initialised the array i.e. "dear compiler, I want 10 slots for things in the array, make it so!".

    Code (csharp):
    1.  
    2. function Start()
    3. {
    4. ...
    5. buttons = new Sprite[10];
    6. ...
    7.  
     
  5. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    But heres the thing, the amount of slots or objects there will be will vary greatly.
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    then you're not looking for an array. They are fast, but of a set size. If the numbers of slots is variable you're looking at a list or some other dynamically sized collection type.


    edit: unless you'll know the number of slots before hand, it can be a variable value, "Sprite[x]" but you'll need to know x before you create the array
     
  7. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    I geuss I could have a variable in there saying the total amount of slots. Or I could just use the total length of the array, just to make things simple. But still I need a way to intantiate my array of sprites and put them into the scene
     
    Last edited: May 24, 2015
  8. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,699
    Code (csharp):
    1.  
    2. for(int I = 0; I < someArray.Length; I++){
    3. someArray[I].bringIntoScene();
    4.  
     
  9. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    wait wahh? Is that a built in function for unity or is it referencing something within the array?
     
  10. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,699
    a for() statement runs thru until it reaches a certain number. In this case the number is the length of the array. Each time it runs thru, I instruct each member of the array to come into scene.
     
  11. SnakeTheRipper

    SnakeTheRipper

    Joined:
    Dec 31, 2014
    Posts:
    136
    For each item in the array, Instantiate the array item.

    Code (CSharp):
    1. foreach (GameObject go in array)
    2. {
    3.      Instantiate(go, Vector3.zero, Quaternion.identity);
    4. }