Passing in multi-typed data structure into scripts parameters

So I’m creating a Enemy Generator Class, which spawns enemies at specific times. So it has to know how many of that enemy to spawn, which enemy it’d like to spawn and at what time. I’d use this Generator Classes on each of the cardinal directions on the camera. The data structure to be passed into the Class would be something like:

[
  [
    time => 60
    enemies => [{
        numberToSpawn: 10,
        enemyType: 'Peasant'
      },
      {
        numberToSpawn: 5,
        enemyType: 'Town Guard'
      }
    ]
  ],
  [
    time => 70
    enemies => [{
        numberToSpawn: 5,
        enemyType: 'Town Guard'
      },
      {
        numberToSpawn: 5,
        enemyType: 'Rookie'
      }
    ]
  ]
]

What’s the best approach to passing the data in like this so I can configure it on the fly?

Update: I think i might be on the right lines, correct me if I’m wrong, I’ve thought about creating a new Enemy Generator Data Class and then pass this into my Enemy Generator Class as a List. So this would be a property on my Enemy Generator Class:

 public List<EnemyGeneratorDataLogic> list = new List<EnemyGeneratorDataLogic>();

Second Update:
So now I have 3 Classes:
Enemy Generator,
Enemy Generator Data,
Enemy Data.

So the Classes look like this:

public class EnemyGeneratorLogic : MonoBehaviour
{
    public List<EnemyGeneratorDataLogic> enemyGeneratorData = new List<EnemyGeneratorDataLogic>();

    void Start()
    {
      
    }

    // Update is called once per frame
    void Update()
    {
      
    }
}

So that takes a list of EnemyGeneratorData. The EnemyGeneratorData then has these two properties on it:

public class EnemyGeneratorDataLogic : MonoBehaviour
{ 
     public float time;
     public List<EnemyDataLogic> enemyData = new List<EnemyDataLogic>();
}

The last Class then sets the data for the enemy data, so the number of enemies and the enemy type:

public class EnemyDataLogic : MonoBehaviour
{
    public int numberOfEnemies;
    public string nameOfEnemy;
}

Be nice if you didn’t have to actually create separate Classes to do this. you could just say on the Class I want an array, in the array I want a mix of data types like arrays, array of objects, string, int etc.

Talking into the void helps, hope this helps someone else :slight_smile:

Few advises. Just because you are using Unity doesn’t mean that every class needs to be a mono behavior. A lot of general C# rules and features still apply (with some exceptions and limitations caused by the version of C# unity supports). If you limit yourself only to Unity tutorials without actually learning C# you can easily miss a lot of stuff.

Tuples do exist.

Strict rule of single class/c# file only applies to certain classes inheriting from unity object like MonoBehavior and Scriptable object. It should generally still be followed for rest of c# code, but as with many style rules in programming there is some choice of whether they make sense and benefit in specific situation. With hierarchy of small data classes like this (assuming they are not mono behaviors) intended to only be used for defining specific nested structures you might consider declaring them together.

You might also want to look into scriptable objects.

I took your advice, I change all my instances to derive as a scriptable object, so now the Enemy Generator Data and
Enemy Data classes have been changed to scriptable objects. This way looks way cleaner, thanks :slight_smile: - nice how you can also add them to the asset menu, makes setting stuff up quick!