Instantiating object A randomly at one of two locations and object B at the other of the two locations

Hey,

I’m relatively new at Unity (worked through the Unity Development Essentials guide book) and I’m working on developing my first game.

I’d like to instantiate randomly a pair of two distinct, but linked (if one is randomly selected to be instantiated, then the other must be too) objects. The player will shoot at only one of them, and I’d like the one he shoots at to be randomly on the left or the right, i.e. I need to instantiate their locations randomly at one of two places, but I don’t want them both to end up in the same location!

Thanks in advance.

Here’s what I have so far:

#pragma strict
var attackElves : GameObject[];
var attackPositions : Transform[];
var fatElfCompanion : GameObject;
var shortElfCompanion : GameObject;
var strongElfCompanion : GameObject;
var smartElfCompanion : GameObject;
var fastElfCompanion : GameObject;
var toughElfCompanion : GameObject;
var uglyElfCompanion : GameObject;
var youngElfCompanion : GameObject;

//Populate array with enemy elves

function Start () {

}

function Update () {

}

function OnTriggerEnter(col : Collider)
{
	if(col.gameObject.tag == "Player")
	{
		yield new WaitForSeconds(3.0);
		Instantiate(attackElves[(Random.Range(0, attackElves.Length))], attackPositions[(Random.Range(0, attackPositions.Length))], Quaternion.identity)
		if(Instantiate.attackElves.tag == "fatElf")
		{
			Instantiate(fatElfCompanion

import System.Linq;

 ...

  private var availablePositions : List<Transform>;

  function Start() {
       availablePositions = attackPositions.ToList();
}

function OnTriggerEnter(col : Collider)
{
  if(col.gameObject.tag == "Player")
  {
    yield new WaitForSeconds(3.0);
    var position = availablePositions[Random.Range(0, availablePositions.Length)];
    availablePositions.Remove(position);
    Instantiate(attackElves[(Random.Range(0, attackElves.Length))], position, Quaternion.identity);
    //Now availablePositions only contains the positions that are not used

Mike,
I’m sorry to bother you again. I tried to use “List” in conjunction with import.System.Linq;, and I got an error message: “Unexpected token: <”, highlighting the “var availablePositions: List;” line. Any idea what I’m doing wrong? Thanks.

Here’s the script:

import System.Linq;
  import System.Collections.Generic;


var availablePositions : List<Transform>;
//Allows assignation through the Inspector of the two possible positions from which the Spawn function will
//later randomly select.
var blackElves : GameObject[];
//Populates array with enemy elves
var fatElfCompanion : GameObject;
var shortElfCompanion : GameObject;
var strongElfCompanion : GameObject;
var smartElfCompanion : GameObject;
var fastElfCompanion : GameObject;
var toughElfCompanion : GameObject;
var uglyElfCompanion : GameObject;
var youngElfCompanion : GameObject;
//links each enemy elf with a partner


function Start () 
{
 availablePositions = attackPositions.ToList();
}
//lists available attack positions of enemy elf and his partner.

function Update () {

} 

function OnTriggerEnter(col : Collider)
{
 if(col.gameObject.tag == "Player")
 {
 yield new WaitForSeconds(3.0);
 Spawn;
 }
}
//When player enters this zone, the function "Spawn" is called after three seconds.

function Spawn ()
{
 var position = availablePositions[Random.Range(0, availablePositions.Length)];
 availablePositions.Remove(position);
 //One position is selected at random from two possibilities. The position selected is removed from set of possibilities.
 var attackElf = Instantiate(blackElves[(Random.Range(0, blackElves.Length))], position, Quaternion.identity);
 //One of the enemy elves is selected from the array and named "attack Elf". It is assigned to the randomly selected position.
 if(attackElf.tag == "fatElf")
 {
 var companionElf = Instantiate(fatElfCompanion, availablePositions, Quaternion.identity);
 }else if(attackElf.tag == "shortElf")
 {
 var companionElf = Instantiate(shortElfCompanion, availablePositions, Quarternion.identity);
 }else if(attackElf.tag == "strongElf")
 {
 var companionElf = Instantiate(strongElfCompanion, availablePositions, Quaternion.identity);
 }else if(attackElf.tag == "smartElf")
 {
 var companionElf = Instantiate(smartElfCompanion, availablePositions, Quaternion.identity);
 }else if(attackElf.tag == "fastElf")
 {
 var companionElf = Instantiate(fastElfCompanion, availablePositions, Quaternion.identity);
 }else if(attackElf.tag == "toughElf")
 {
 var companionElf = Instantiate(toughElfCompanion, availablePositions, Quaternion.identity);
 }else if(attackElf.tag == "uglyElf")
 {
 var companionElf = Instantiate(uglyElfCompanion, availablePositions, Quaternion.identity);
 }else(attackElf.tag == "youngElf")
 {
 var companionElf = Instantiate(youngElfCompanion, availablePositions, Quaternion.identity);
 }
 //A second, "Companion Elf" is instantiated at the other remaining position. It is not selected at random. It
 //must be the concomitant of the "Attack Elf" previously selected.
}

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”);
}

}