Loading new level after all enemies are dead?

I saw a topic on here about that, and it didn’t help me.

I have a game object, named “Enemies”, that contains children named “SmallTurret”.

Here’s my script that successfully destroys the children:

var explosion : Transform;
var hitSound : AudioClip;
var hitSoundStillAlive : AudioClip;
var isSmall = false;
var isLarge = false;
var health = 2;


function OnTriggerEnter( hit : Collider )
{
	if(hit.gameObject.tag == "wormProjectile")
	{
		if(isSmall)
		{
			AudioSource.PlayClipAtPoint(hitSound, transform.position, 10);
	
			Destroy(hit.gameObject);
	
			var exp = Instantiate(explosion, gameObject.transform.position, 
							Quaternion.identity);
			Destroy(gameObject);
		}
		
		if(isLarge)
		{
			
			
			AudioSource.PlayClipAtPoint(hitSoundStillAlive, transform.position, 10);
			Destroy(hit.gameObject);
			
			health--;
			
			if(health == 0)
			{
				AudioSource.PlayClipAtPoint(hitSound, transform.position, 10);
				exp = Instantiate(explosion, gameObject.transform.position, 
								Quaternion.identity);
				Destroy(gameObject);
			}
			
		}
	}
}

The script is attached to “topPart”, which is a child of “SmallTurret”.

I read that I’m supposed to check if the parent has any more children each time an enemy is destroyed. I don’t know how to do that.

I’d like some help with this basic code.

Retrieve all components of type X (your script ?), as a list.

If the length of the list is equal to 1 right before deleting a turret…

This is really stressing me out. I understand what I’m supposed to do - check the parent for children and if it has 1 right before the last one is destroyed, then the next level loads.

But I’m confused about where to put this GetComponentsInChildren code. I take it it goes simply in the script I attach to each enemy. But that way, how does it know to go to the parent to check for children?

I don’t understand what the parameter “HingeJoint” is supposed to be. I believe that is the parent that is checked, so I tried using the name of the parent and the tag of the parent, but it didn’t work.

Just don’t go copy paste code by hoping it is gonna work ! :?

It may be more adequate. Get the game object Enemies. Get all components SmallTurret (if your script is named SmallTurret).

public var enemiesRoot : GameObject;

var smallTurretList : SmallTurret [];
smallTurretList = enemiesRoot.GetComponentsInChildren (SmallTurret);

I don’t understand those pages on the support site. They just show code that doesn’t make sense to me, and I like to know what it means and how it is used.

I don’t understand why you are instantiating an array of type SmallTurret (script name). And it looks like the parameter of GetComponentsInChildren should be the parent (Enemies). I’m so confused.

It returns a list of components of a precise type in the game object and any of its children.

If there are none, what do you do ?

You have to tell your code everything. If you’re worried that a Turret won’t know which Enemy to report to, -tell- it which Enemy to report to.

Each Enemy should be capable of finding its own Turrets and telling them that it is their Enemy; then they can easily tell it when they blow up and it can easily keep track of how many are remaining.

gameObject.GetComponentsInChildren(X) will return :
gameObject’s X component
all X components in all children of gameObject

Enemy.js

// How many turrets remaining
private var turretCount : int = 0;

function Start() {
 ...
 // Initialize our turretCount
 // Find all Turret children we have and count them
 // GetComponentsInChildren return an Array filled with Turret objects.
 var turrets = GetComponentsInChildren(Turret).length;
 for ( turret : Turret in turrets ) {
   turret.SetEnemy( this );
 }
 turretCount = turrets.length;
 ...
}
function TurretDestroyed() {
 turretCount -= 1;
 if ( turretCount == 0 ) IAmDestroyed();
}
Turret.js

private var myEnemy : Enemy;

function SetEnemy( enemy : Enemy ) {
 myEnemy = enemy;
}

function Destroyed() {
 myEnemy.TurretDestroyed();
}