This is the final, working script:
#pragma strict
import System.Linq;
import System.Collections.Generic;
var blackElves : List.<GameObject>;
//This populates an array to which all possible black elf prefabs which the user is to shoot can be assigned
var companionElves : GameObject[];
//This populates an array to which all possible black elf prefabs which the white elf companion is to shoot can be assigned
var blackElfTags : String[];
////This populates an array to which the tags of all instantiated black elfves which the user is to shoot can be assigned. This will later enable
//the matching of the black elves which the user is to shoot with the companion elves which the white elf is to shoot.
var availablePositions : Transform[];
//The left and right positions at which the black, attacking elves may be instantiated are here listed in the Inspector and remain unchanged in the
//script. They are referenced later as the starting points from which the attack elves begin their uphill charge.
var availablePositionsTEMP : List.<int>;
//The left and right attack positions are copied into this list during the script. The first position selected randomly is subtracted from this array
//so that the second position selected cannot be the same. Both are cleared out, and the list is repopulated after every call of the Spawn function.
//This ensures that the list is always identical to the availablePositions list at the beginning of each call of the Spawn function and that it not
//be cluttered with the uncleared, secondarily selected positions from previous calls of the Spawn function.
function Start ()
{
}
function Update ()
{
}
function OnTriggerEnter(col : Collider)
{
if(col.gameObject.tag == "Player")
{
yield new WaitForSeconds(4.0);
Spawn();
}
}
//When the player enters this zone, the function "Spawn" is called after three seconds.
//Purpose of Spawn():
//We want two transform locations at which we want to instantiate a pair of attack elves. We want one attack elf to come from the list called
//"blackElves" and we want the other to come from the array called "companionElves". For each elf in the "blackElves" list there is one and only one
//, matching elf in the array "companionElves". When a given blackElves elf is instantiated, its concomitant from companionElves should also be
//instantiated. We do not want the blackElves elf to appear always at the same
//transform location. We do not want the blackElves elf and the companionElves elf to appear at the same transform location. We want whichever elf,
//black or companion, which appears at the left transform to advance along the leftward path. We want whichever elf, black or companion, which
//appears at the right transform to advance along the rightward path.
//We have dropped the two transform locations into the array called "availablePositions" in the Inspector. These remain static throughout run-time
//to provide a definite reference point when assigning each elf a path of advance (if at the left, leftward advance; if at the right, rightward advance).
function Spawn()
{
for(var k = 0; k < availablePositions.Length; k++)
{
availablePositionsTEMP.Add(k);
}
//The script needs to refer back to one of the two transforms listed in availablePositions by their index numbers, which here happen to be just
//"0" and "1" (were availablePositions larger, then of course there would be more index numbers). It creates a new generic list, called
//"availablePositionsTEMP" which contains the same number of indices as the original, availablePositions table. Rather than refer to a transform,
//however, each index refers to a number, which matches the index numbers in availablePositions and which *there* refers to a transform location.
var intA = availablePositionsTEMP[Random.Range(0, availablePositionsTEMP.Count)];
//The script chooses the value of one index of the availablePositionsTEMP list, from the first index (0) and ending with the last index
//(whatever the last number of the table) and assigns it to the variable "intA". It so happens that the indices and values of availablePositionsTEMP
//are equal (0,0; 1,1; etc.).
var positionA = availablePositions[intA];
//The value of intA, a number, corresponds to an index of the table "availablePositions", which in turn, refers to a transform position contained
//in that array. The *value* of that index in availablePositions, a transform, is assigned to the new variable "PositionA".
availablePositionsTEMP.Remove(intA);
//The script removes the index that was randomly assigned to "intA" from the list availablePositionsTEMP. That way, the script won't
//select the same value when it assigns a position to the second attacking elf, from the array "companionElves".
var elfInt = Random.Range(0, blackElves.Count);
//The script selects an integer randomly from "0" to whatever number corresponds to the length or "count" of the list "blackElves" and assigns
//that value to the variable "elfInt".
var attackElf = Instantiate(blackElves[elfInt], positionA.position, Quaternion.identity);
//The script instantiates an attacking elf from the list "blackElves". It does so randomly by selecting the index whose number corresponds to
//whatever the value that was randomly assigned to the variable "elfInt" in the previous command. The script instantiates the elf at positionA,
//which was selected randomly above and gives it no rotation.
var intB = availablePositionsTEMP[(Random.Range(0, availablePositionsTEMP.Count))];
//As the value of the availablePositionsTEMP list at the index "intA" have already been removed, the script has only one value left to select
//(in the present script: had availablePositions more than two values, this would not be true). This value it chooses and assigns to the variable
//"intB".
var positionB = availablePositions[intB];
//The value of intB, a number, corresponds to an index of the table "availablePositions", which in turn, refers to a transform position contained
//in that array. The *value* of that index in availablePositions, a transform, is assigned to the new variable "PositionB".
blackElves.RemoveAt(elfInt);
//The script removes the gameObject at the index which corresponds to the number "elfInt" (randomly selected and instantiated above). That way,
//the user does not have to shoot the same elf again the next time the Spawn function is called. Once all elves in the blackElf list have been
//shot and eliminiated, the mission is over and the user has won.
//NOTE: The script removes the value AT a certain index, rather than the value itself. This is because "elfInt" is a number and therefore is
//equal only to the index, which is also a number, not the value, which in this list is a gameObject.
for(var i = 0; i < blackElfTags.length; i++)
{
if(attackElf.tag == blackElfTags*)*
{
var companionElf = Instantiate(companionElves*, positionB.position, Quaternion.identity);*
break;
}
}
//To instantiate the correct companionElves elf, the script searches (for loop) the array called “blackElfTags” and looks for the index number
//whose field (a tag) matches the tag of the instantiated blackElves elf. The companionElves are listed in the same order as their partners
//in the blackElfTags array. The script therefore is to instantiate whichever companionElves elf is located at the same index number as the
//the index number of the blackElfTags array whose value (tag) matched the tag of the instantiated attacking blackElves elf. The elf is instantiated
//at positionB (whichever position remains from the array availablePositions) and given no rotation.
availablePositionsTEMP.RemoveRange(0,availablePositionsTEMP.Count);
//All indices are removed from the list availablePositionsTEMP. The list is now empty and ready to be populated as the first time during the next
//call of the Spawn function.
if(positionA.transform == availablePositions[0])
{
attackElf.AddComponent(“PathLeft”);
}else{
attackElf.AddComponent(“PathRight”);
}
if(positionB.transform == availablePositions[0])
{
companionElf.AddComponent(“PathLeft”);
}else{
companionElf.AddComponent(“PathRight”);
}
}