Picking random enemy; constructing objects name

I have a wave of Invader-type enemies onscreen. I have a function to randomly select one to carry out an attack process i.e shoot bullet, dive at player etc.
The enemies are named as Enemy1 through to Enemy20 and the randomly selected index number is rndEnemy. I was hoping to construct the Enemy objects name into a String variable called ‘attacker’ a little like this:

var attacker : String=“Enemy”+ rndEnemy
resulting in a String like i.e Enemy10 etc. Is this the right way to go or is there a simpler method? If this is a good way how can I turn the String var into an Object name?

I think it would be better if you had all the enemies in an array; then you could simply use a random index number.

–Eric

You’re right, I haven’t tried arrays with game objects yet. So after declaring the array

var Enemy : GameObject[];

do I insert the 20 Enemy objects into the array and name them within a for/next loop ? A quick code example would be welcome, I’m away from my Unity comp right now and I’d like to look into this later tonight.

Insert them however you’d like, really. You can pick a random one by just doing something like this:

curTarget = Enemy[Random.value * Enemy.length];

Gargerath’s suggestion is almost right, but there is a slight sting in the tail with Unity’s Random.value. Unlike almost every other random number function, it can actually return a value of 1, so if you use Random.value * enemy.Length, you will occasionally get an index out of range. You can, however, use Random.Range:-

target = enemy[Random.Range(0, enemy.Length)];

Thanks for all the help. Sorry to say I’m having a struggle with the Array of game objects. I can see exactly how they work and what a huge benefit they will be, I just need to absorb how the Array holds these objects and how to access them. I have this code to create and position my enemy objects…

var enemy : GameObject[]; // My array set up to hold 20 Enemy prefab objects
var enemyObj : Transform;   // This is my Enemy prefab

// Now a simple loop to create/position them
function Start () {
        for (var x = 1; x < 21; x +=1) {
                 var enemyObj : Transform = Instantiate (enemyObj, Vector3(x,0,0) , Quaternion.identity);                
               }   
}

This is where I start to lose my way…I can drag the prefab into the exposed variable slots in the inspector, but that is just the prefab and its basic properties. I need to name each of the 20 enemies in the loop and insert them into the Array from there.Is that how it works? I have a modified loop which merges the prefix 'Enemy 'and the current loop index, so I have the objects named from Enemy1 to Enemy 20 for instance, but how do I insert them into the array so that the command print (enemy[10] will actually print “Enemy10” and allow me to access the enemy[10] instance to modify its movement etc ?

An array is just a list of several different variables of the same type. If you write something like:-

x = arr[2];

…then it will set x to the value of element 2 - the number in the square brackets is known as the index. Something to be aware of is that the index numbers of arrays begin at 0 rather than 1, so arr[2] is actually the third element in the sequence, not the second. For the same reason, the last index in the array is equal to arr.Length - 1. If you have an array of twenty elements, you loop through them with:-

for (i = 0; i < 20; i++) {
    // Something involving arr[i]...
}

If you declare a public array variable in Unity, you can add items to the list in the inspector panel just by setting the values or dragging objects in. I’m not quite sure what you intend to do in your example, but you might be able to avoid naming your objects in the code - just add them to the array in the inspector and name them from there.

Thanks andeee. Yeah, I’m ok with arrays and used them extensively in the past. It’s this idea of storing objects and the like within them. I’ve fiddled with my code and I have them stored correctly, I also took the zero indexing you mentioned into account. I’m just struggling to address them correctly; I need to get for example Enemy10 from the array called Enemy containing 20 such named objects. Something like;

 Alien [10].transform.Rotate(1, 0, 0);

bearing in mind this is also for iPhone so may require strict typing ?