At the end of FPS tutorial level...

Id like something to happen, once all robots and sentry guns are wasted in th fps tutorial. Specifically, Id like to load a specific level, or even enable a gui. So how would I go about recognising that all enemies are detroyed?

function Update(){
if (all robots and sentryguns are destroyed(robots and sentryguns));
Application.Loadlevel("You Won");
}

Obviously this wont work, but you get the idea
Totally digging the new release guys
AC

One way would be to have a static variable somewhere with the total number of enemies, and each time one is destroyed, subtract one from the number. When it’s 0, there you go.

–Eric

If the enemies all have an “Enemy” tag, you can use GameObjectsWithTag, and test the length of that array. When it’s zero, you’re done.

var enemies : GameObject[];
var enemies = GameObject.FindGameObjectsWithTag("myTag");

if (enemies.Length <= 0)
{
    //all are dead
}

(I code in C#, but I think that is correct :wink: I didn’t use JS long enough to use the : operator, and my JS array knowledge is rusty)

-Jeremy

var enemies = GameObject.FindGameObjectsWithTag("myTag"); 

if (enemies.Length <= 0) 
{ 
    //all are dead 
}

The first line was unnecessary (in fact it would trigger an error, since in the next line you declare “enemies” again :slight_smile: )

JS is not strongly typed.

Thanks guys but I cant seem to get it working either way.

var enemies = GameObject.FindGameObjectsWithTag("myTag"); 
function Update(){
	if (enemies.Length <= 0) 
 { 
    Application.LoadLevel ("YouWon"); 
} 
}
var enemies = GameObject.FindGameObjectsWithTag("myTag");
function Update(){
if (enemies.Length <= 0) 
{ 
   Application.LoadLevel ("YouWon"); 
}
}

Both are no go’s, but maybe I’m missing something?
Thanks though
AC

A

Heh, thanks man, I figured I’d give Targos a JS code sample to work from and let the JS users correct me :wink:

-Jeremy

Targos,
Did you create a new tag in Unity and set your object to that tag?

This line:

GameObject.FindGameObjectsWithTag("myTag");

Is saying find all game objects that have this tag assigned to them: then the name of the tag.

Make sure you set a new tag and change the name in that code. Check the docs for how to set tags.

-Jeremy

Apart from indentation, both versions look identical.
In both versions the list of enemies is only fetched at game start. You will need to refresh the list inside the Update function:

 function Update(){
  var enemies = GameObject.FindGameObjectsWithTag("myTag");
  if (enemies.Length <= 0) 
  { 
    Application.LoadLevel ("YouWon"); 
  } 
}

Ok now its compiling…
Thanks team-I havent really done anything where the varibiles are inside the Update function, only outside. I’ll give this a go, it should work now
Thanks
AC