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

Creating new arrays through the Inspector.

Discussion in 'Scripting' started by salehi-gamedesign, Apr 25, 2020.

  1. salehi-gamedesign

    salehi-gamedesign

    Joined:
    Apr 30, 2018
    Posts:
    15
    I'm trying to program a script which is attached to a GameObject so that it displays an integer entry box in the Unity Inspector so that when the user enters a number greater than 0, it then creates the stated number of GameObject arrays beneath it. The length of these created arrays should also be configurable with each having their own integer entry box within the Inspector which allows the user to define the length of each of those individual arrays.

    I am able to create arrays which are displayed in the Inspector by writing them into the script individually e.g.:
    Code (CSharp):
    1. public GameObject[] wave1Enemies;
    2. public GameObject[] wave2Enemies;
    upload_2020-4-25_19-14-42.png

    However I would like to make this configurable through the Inspector if possible instead of having to return to the script to add additional arrays.

    Unfortunately I have no idea how to approach this and my searches haven't yielded anything useful, perhaps because I'm lacking the correct terminology to describe what I'm trying to create.

    Any guidance would be appreciated.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    You'll need to nest some serializable classes, something like:

    Code (csharp):
    1. [System.Serializable]
    2. public class GameObjectArray
    3. {
    4.   public GameObject[] GameObjects;
    5. }
    And then have a varying array of those.

    Code (csharp):
    1.  
    2. public GameObjectArray[] Arrays;
    3.  
     
    salehi-gamedesign likes this.
  3. salehi-gamedesign

    salehi-gamedesign

    Joined:
    Apr 30, 2018
    Posts:
    15
    Hi Kurt, thanks for your reply.

    I finally had some time to tinker with this and it seems perfect, much appreciated!
     
    Kurt-Dekker likes this.