Building an array help

I’m trying to build an array of targets for the ai to attack. There are three teams A B and C and I’m trying to get team C to attack either B or A at random by picking them from an array list. I can build an array for C to attack either team A or B but bot both. Heres what I have so far:

var possibleTargets = new Array();
function Start()
{
     possibleTargets.Add(GameObject.FindGameObjectsWithTag("HumanA"));
	 possibleTargets.Add(GameObject.FindGameObjectsWithTag("HumanB"));
	  target = possibleTargets[Random.Range(0, possibleTargets.length)].transform;
	}

function Update () {

if (target==null){
target= possibleTargets[Random.Range(0, possibleTargets.length)].transform;
}

with this way i don’t get any errors but it also doesn’t pick a target. If it picks a target everything else in the script works fine. Hope somebody can point me in the right direction! Thanks!

EDIT:

I tried this function and it didn’t work either. target is still coming back null and the possibleTargets is coming back as just a list of arrays.

var possibleTargets = new Array();

function Start()
{
var tempTargets = GameObject.FindGameObjectsWithTag(“HumanA”);
for(gameObject in tempTargets)
{
possibleTargets.Add(tempTargets);
print(possibleTargets.length);
}

tempTargets = GameObject.FindGameObjectsWithTag("HumanB");
for(gameObject in tempTargets)
{
possibleTargets.Add(tempTargets);
}
target = possibleTargets[Random.Range(0, possibleTargets.length)].transform;
print(target);
}

The best advice I can think of is to add a few print or debug.log statements (those are c#, I’m not sure what the equivalent statements are in javascript) which display possibleTargets.length after each attempt to add a new gameObject to the array, just to make sure that the array is actually being built properly.

Though the issue is (I think, I could be incorrect and I’m sure someone will correct me if that’s the case)that you’re making target a transform type instead of a GameObject. If my understanding of that is correct, you’re only getting the transform of the initial target, and only the transform that it was in when the target was assigned. This being said, your variable target will NEVER be null because destroying the current target will not change the value of the transform variable you have stored.

My suggestion is to try changing your target variable into a GameObject and then call target.transform whenever you need to work with the current location of the target(ie in your pathfinding/seeker script.) This will also cause target==null when the target is no longer in existance.

Sorry for the long winded answer with no concrete code example, but unless my understanding of the way a transform variable is treated, you should have better success if you try this out.

EDIT:

instead of

var possibleTargets = new Array();
function Start()
{
     possibleTargets.Add(GameObject.FindGameObjectsWithTag("HumanA"));
     possibleTargets.Add(GameObject.FindGameObjectsWithTag("HumanB"));
      target = possibleTargets[Random.Range(0, possibleTargets.length)].transform;
    }

try something like

var possibleTargets = new Array();
function Start()
{
   var tempTargets=GameObject.FindGameObjectsWithTag("HumanA");

   foreach(gameObject temp in tempTargets)
           {
               possibleTargets.add(temp);
           }

   tempTargets=GameObject.FindGameObjectsWithTag("HumanB");
           {
               possibleTargets.add(temp);
           }

}

I’ll admit that I really have no idea what the syntax would be for js, so i used the c# foreach statement, but I’m sure something similar exists in js.Before you were essentially creating an array of arrays if my understanding of your comment is corrent.

Hope this helps!

Here’s an easier approach:

var possibleTargets : GameObject[];

function Start()
{
     possibleTargets = GameObject.FindGameObjectsWithTag("HumanA")
        + GameObject.FindGameObjectsWithTag("HumanB");
	 target = possibleTargets[Random.Range(0, possibleTargets.Length)].transform;
}

function Update ()
{
    if (!target)
        target= possibleTargets[Random.Range(0, possibleTargets.Length)].transform;
}

Two tricks here: array addition (JavaScript only, don’t try in C#), and using the Boolean operator of GameObject to know when it has been destroyed (comparing with null is not the same thing).