Search Unity

Adding Arrays to List at Runtime

Discussion in 'Scripting' started by Crestwood, Dec 3, 2019.

  1. Crestwood

    Crestwood

    Joined:
    Dec 29, 2017
    Posts:
    2
    Hello all. I am trying to avoid manually adding a lot of arrays to a list manually, but not sure of the best way to do this. I have read a bit on reflection, but trying to avoid it. Here is a sample of what I am trying to do.

    I have a game that starts at level 1 and will continue to go up as the player progresses. I have an array of objects for each level. In the arrays i assign the prefabs for the levels. I planned to create a list to then store all of the arrays in order by level. I was hoping to create a loop to add all the arrays at start.
    My initial thought was

    Code (CSharp):
    1.     public GameObject[] level_2;
    2.     public GameObject[] level_3;
    3.     public GameObject[] level_4;
    4.     public GameObject[] level_5;
    5.     public GameObject[] level_6;
    6.     public GameObject[] level_7;
    7.     public GameObject[] level_8;
    8.     public GameObject[] level_9;
    9.     public GameObject[] level_10;
    10.     public List<GameObject[]> levelsList = new List<GameObject[]>();
    11.  
    12.  
    13.     void Start()
    14.     {
    15.         for(int i=0;i<11;i++)
    16.         {
    17.             levelsList.Add("level_ "i);
    18.         }
    But converting string to object type is not possible. Doing it for 10 levels is easy, but if it gets up to 100 it might be a pain. Any advice would be helpful.

    Thanks!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    4,589
    You want to get away from making actual identifiers such as Level_1 and Level_2 and so on because then you need to write code whenever you add a level, and that is a VERY 1990s approach.

    Instead, check out ScriptableObjects for defining pre-made world data like this.

    In your case I imagine you would have one called LevelDefinition:

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. [CreateAssetMenu]
    4. public class LevelDefinition: ScriptableObject
    5. {
    6.   public GameObject[] PrefabsForLevel;
    7. }
    You would make each level and drag whatever GameObjects you want into it.

    Finally you'd have an array of those in your GameManager that you would drag the predefined levels into.

    There are tons of ScriptableObject tutorials out there, but here are the basics:

    - they are just a bag of data
    - they can be edited directly in the Unity editor like any prefab or other item
    - each instance maps to one asset file on disk (such as Level1.asset for instance)
    - the 'CreateAssetMenu' decorator above lets you make them by right-clicking in the project panel

    However, if you JUST want to store levels, such as sequential level content, I strongly suggest using scenes, and loading those scenes additively. It is SUPER-clean and easy.

    When actually playing the game, you might have, for instance:

    - the player scene
    - the main UI scene
    - the content scene

    Each of those would be additively loaded so all three are in-engine at once. And it still might be useful to define stuff with ScriptableObjects, so these two things are not mutually-exclusive; you sorta have to tinker with them to get a feel for what they can give you.
     
  3. Crestwood

    Crestwood

    Joined:
    Dec 29, 2017
    Posts:
    2
    Ahh ok. Will look into ScriptableObjects.