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

Expose array in editor for manual population

Discussion in 'Scripting' started by guitarxe, Apr 15, 2014.

  1. guitarxe

    guitarxe

    Joined:
    Dec 1, 2013
    Posts:
    131
    Hi, I'd like some help with something, please. I'm a beginner programmer and Unity3D user.
    This is what I'm trying to accomplish:

    I have an undetermined number of prefabs.
    I wanted to expose an array to the editor, so that I could drag prefabs into the array, as many prefabs as I would need.
    Then, I could deal with the prefabs through the array for instantiating and positioning.

    I wanted to do it this way because I didn't want to manually write out lines of
    Code (csharp):
    1. Instantiate(Prefab1);
    2. Instantiate(Prefab2);
    And so on for each prefab. Instead I could loop through the array and instantiate each prefab in there.

    However, it doesn't seem like this is possible? If I just do
    Code (csharp):
    1. public Transform[] listOfPrefabs;
    Then the only thing exposed in the editor is the size of the array. I can't actually populate it element by element as I was hoping would be possible.

    Is it possible by any other means? If not, what would be a convenient way of instantiating multiple prefabs, without doing it individually line by line?
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Well, just type the size of the array you want, and you will be able to drag drop stuff in it?
     
  3. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    If you drop multiple objects onto an array field then they should all get added at once.

    I cannot remember whether this only works with an empty array, but I am almost certain that this does work.
     
  4. guitarxe

    guitarxe

    Joined:
    Dec 1, 2013
    Posts:
    131
    /facepalm I'm dumb, thanks.