Trying to be more efficient. Advice please...


I have a script which sets up values for different Enemy Craft and passes the variables onto a spawned prefab, etc. An example of the variable structure is a bit like this:

Enemy 1-----|Health
            |Turn Speed
            |Move Speed
            |Fly Mode

 //Not actual code but a rough example of initial Var setup.
//The actually script is MUCH bigger, but this is an accurate representation of the structure at the moment.
    var enemy1Prefab : Transform;
    var enemy1Health : int;
    var enemy1TurnSpeed : float;
    var enemy1MoveSpeed : float;
    var enemy1FlyMode : boolean;

Everything works great. However, each time I want to add a new enemy, I have to copy/paste/modify chunks of code. Currently I am at 5 enemy and the script is getting huge. This got me thinking that there must be a more efficient way to do this and save time / reduce the amount of errors created with lots of editing. I am thinking an array is probably the best way to go…

I have recently learnt about List arrays, but I am not sure if they are the right way to go. Here are some specific questions:

1 - My goal is to be able to say in the Inspector that I want to create 10 enemies, when I do that 10 slots are created where I can enter the variable values for health etc. Is this the right direction to go?

2 - If an array is the way to go, am I looking at what is called a multidimensional array? Any advice on best practices for this type of thing?

3 - I am even considering about trying my hand at creating a custom editor (Unity - Scripting API: Editor). Is this overkill for my needs?

Hopefully someone can put me on the right path to being more efficient!



What you can do is to create a custom struct or class which contains all of the data members you need… Then, you can create ten instances of your struct, and each one can be tweaked individually. Here’s how it would look:

class enemy
    var myPrefab : Transform;
    var myHealth : int;
    var myTurnSpeed : float;
    var myMoveSpeed : float;
    var myFlyMode : boolean;
    // ... etc

var enemy1 = new enemy();
var enemy2 = new enemy();
// ... etc

each instance you create (enemy1, enemy2, etc) will have its own complete set of the vars… Then, when you need to add a new enemy, just add another instance…